spacer
Yehuda Shiran July 2, 2001
Setting an Attribute Node in IE6 and NS6
Tips: July 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

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

Unlike Internet Explorer 5.5 that supports only setAttribute() and getAttribute(), Internet Explorer 6 (as well as Netscape 6) supports also setAttributeNode() and getAttributeNode(). The setAttributeNode() adds an attribute object to an existing DOM node. Here is the syntax:

elementNode.setAttributeNode(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 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 tip:

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)));
}


People who read this tip also read these tips:

Look for similar tips by subject:

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