Netscape 6, Part IV: DOM Differences and Commonalities with IE5.x : Creating and Removing Attributes - Doc JavaScript | WebReference

Netscape 6, Part IV: DOM Differences and Commonalities with IE5.x : Creating and Removing Attributes - Doc JavaScript


Netscape 6, Part IV: DOM Differences and Commonalities with IE5.x

Creating and Removing Attributes

Netscape 6 lets you create attributes on the fly. For example, to create a node foo, you would write:

document.createAttribute("foo");

Both Netscape 6 and Internet Explorer can remove attributes from other elements, such as the P tag. The first P element on this page 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. And indeed, when clicking the button below, 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 the script in lowercase, no matter how write them in the HTML code.

Netscape 6 supports also removing attributes from some elements that Internet Explorer does not support. Specifically, applying the removeAttribute() method to the BODY element works well in Netscape 6 but does not work in Internet Explorer 5.5. This page is colored off white by assigning the BODY attribute, <BODY BGCOLOR="#eeeeee">. We remove this link 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 lightblue 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.

Next: How to create document fragments

http://www.internet.com


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: January 15, 2001
Revised: January 15, 2001

URL: http://www.webreference.com/js/column75/6.html