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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

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:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger