spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column75


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

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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