spacer
Yehuda Shiran November 21, 2000
Removing Attributes
Tips: November 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management
Netscape 6 supports some DOM capabilities that Internet Explorer does not. One of them is removing attributes from elements that won't work with Internet Explorer. Specifically, applying the removeAttribute() method to the BODY element works well in Netscape 6 but doesn't work in Internet Explorer 5.5. This tip is colored yellow by assigning the BODY attribute, <BODY BGCOLOR="yellow">. We remove this background color by the following script:

<SCRIPT LANGUAGE="JavaScript">
<!--
var objBody = document.getElementsByTagName('BODY').item(0);
var remAttr = objBody.removeAttribute('bgcolor'); 
-->
</SCRIPT>

You can see that the background color is still yellow in Internet Explorer 5.5 and lower, but is white in Netscape 6. The reason being that the BGCOLOR attribute has been removed by the removeAttribute() method.

This deficiency in Internet Explorer 5.x does not apply to all element type and attribute combinations. Internet Explorer can remove attributes from other elements, such as the P tag. The first P element on this tip includes the ad at the top of the page. Its only attribute is ALIGN="center". If the browser is able to remove this attribute, we should see the ad changing position from centered to letf-justified. In both Netscape 6 and Internet Explorer 4 and up, you can see that the ad is left-justified. View the source of this page by clicking the View/Page Source menu entries. Notice that the ALIGN="center" attribute is still there. It has been removed when the page loaded, by the following JavaScript script:

<SCRIPT LANGUAGE="JavaScript">
<!--
var objP = document.getElementsByTagName('P').item(0);
var removeAttr = objP.removeAttribute('align'); 
-->
</SCRIPT>
Notice that you need to specify the attribute names in lowercase.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags