spacer

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

home / experts / javascript / column88


Internet Explorer 6, Part I: DOM Standards Support

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Creating attribute nodes

Unlike Internet Explorer 5.5 that supports only setAttribute() and getAttribute(), Internet Explorer 6 (as well as Netscape 6) supports also setAttributeNode(), getAttributeNode(), and removeAttributeNode(). The setAttributeNode() method adds an attribute object to an existing DOM node. The getAttributeNode() method extracts an attribute object from an existing DOM node. The removeAttributeNode() method removes an attribute object from an existing DOM node. Here is the syntax for these three methods:

elementNode.setAttributeNode(attributeObject);
elementNode.getAttributeNode(attributeObject);
elementNode.removeAttributeNode(attributeObject);

where:

  • elementNode is a tag node. It must have been created with the createElement() method.
  • attributeObject is an object that must have been created with the createAttribute() method.

Here is a sample code fragment which first creates an object, nodePublishDate, that is equal to today's date. It then creates a tag element node ("P"), and finally converts this latter node to an attribute node:

var nodeBook, nodePublishDate;
nodePublishDate = document.createAttribute("PublishDate");
nodePublishDate.value = String(Date());
nodeBook = document.createElement("P");
nodeBook.setAttributeNode(nodePublishDate);

We put it in a conditional statement in the header of this page:

if (bVer() >= 6) {
  var nodeBook, nodePublishDate;
  nodePublishDate = document.createAttribute("PublishDate");
  nodePublishDate.value = String(Date());
  nodeBook = document.createElement("P");
  nodeBook.setAttributeNode(nodePublishDate);
}

Click the following buttons to see that indeed nodeBook is an attribute node. The name of the attribute is PublishDate and its value is today's date:

Here is how we define the buttons above:

<FORM>
<INPUT TYPE="button"
  VALUE="getAttribute('PublishDate')"
  onClick="javascript:if (bVer()>=6)
  {alert(nodeBook.getAttribute('PublishDate'))}
  else {alert('You must use Netscape 6 (or higher)
  or Internet Explorer 6 (or higher)')}">
<INPUT TYPE="button"
  VALUE="getAttributeNode('PublishDate').value"
  onClick="javascript:if (bVer()>=6)
  {alert(nodeBook.getAttributeNode('PublishDate').value)}
  else {alert('You must use Netscape 6 (or higher) or
  Internet Explorer 6 (or higher)')}">
</FORM>

The browser version is computed by the bVer() function:

function bVer() {
  // return version number (e.g., 4.03)
  msieIndex = navigator.appVersion.indexOf("MSIE") + 5;
  return(parseFloat(navigator.appVersion.substr
   (msieIndex,3)));
}

Now, click this button to remove the attribute node:

Here is how we define this button:

<FORM>
  <INPUT TYPE="button"
  VALUE="Remove the Attribute Node"
  onClick="javascript:if (bVer()>=6)
  {nodeBook.removeAttributeNode(nodePublishDate)}
  else {alert('You must use Netscape 6 (or higher)
  or Internet Explorer 6 (or higher)')}">
</FORM>

Click the buttons below again and see that the attribute object has been deleted and the date cannot be returned:

Next: How to replace DOM strings

More Resources from Doc JavaScript
Columns Popular Columns Tips Tools
Latest Columns
41-50 | 31-40 | 21-30
11-20 | 1-10
Working with Windows
JavaScript and Frames
Bookmarklets
Random Tips
Personalized Tips
RSS Channels
Menu Builder
Rollover Builder
Rotation Builder
Reference Tip Categories (want one?)
Links
For Your Site
Did you learn something? Do you like this site? Please link to us!

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


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

URL: http://www.webreference.com/js/column88/2.html