November 21, 2000 - Removing Attributes | WebReference

November 21, 2000 - Removing Attributes

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

Yehuda Shiran, Ph.D.
Doc JavaScript

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.