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
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Referencing Objects

The worst habit to get rid of when scripting Netscape 6 is how you reference HTML objects. Internet Explorer has turned us into spoiled scripters. The following HTML tag, for example:

<P ID="foo"><I>Be sure to close this paragraph.</I></P>

renders as follows:

Be sure to close this paragraph.

We can reference this tag in Internet Explorer just by its ID. For example, to figure out its location we state:

foo.style.left

This does not work in Netscape 6. You cannot access objects just by their HTML ID. You need to use:

document.getElementById()

to find the object, and only then compute the object's properties. The following button calls handleClick() upon clicking:

Here is how we define the button:


<INPUT ID="button1" STYLE="position:relative; left:100px;
  visibility:visible;" TYPE="button" VALUE="Show My Location"
  onclick="handleClick()">

And here is how we define the handleClick() function:

<SCRIPT LANGUAGE="JavaScript">
<!--
function handleClick(){
  var obj = document.getElementById("button1");
  alert("horizontal position = " + obj.style.left);
}
// -->
</SCRIPT>

The following code works as well:

<SCRIPT LANGUAGE="JavaScript">
<!--
function handleClick(){
  alert("horizontal position = " +
    document.getElementById("button1").style.left);
}
// -->
</SCRIPT>

Next: How to create and remove attributes on the fly

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: January 15, 2001
Revised: January 15, 2001

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