December 18, 2000 - Initializing Element Coordinates | WebReference

December 18, 2000 - Initializing Element Coordinates

Yehuda Shiran December 18, 2000
Initializing Element Coordinates
Tips: December 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

The browser-independent W3C Standard's way to set and get an element position is via the style object's left and top properties. The following strings are example values of the left and top properties: 50px, 32px, and 0px. You can initialize an element properties by the STYLE attribute of its HTML tag. Notice the left:"150px" string in the following tag definition:

<INPUT ID="button1" STYLE="position:relative; left:"250px"; visibility:'visible';" TYPE="button" 
  VALUE="Show My Visibility" onclick="handleClick1()">
Here is how this button renders:

Click the button to check its visibility. It will come out empty for both Internet Explorer and Netscape 6. Surprisingly enough, both Internet Explorer and Netscape 6 do not recognize the string format of the left and top properties, and position them at coordinates (0,0). To solve this problem, you need to use the integer format:

<INPUT ID="button2" STYLE="position:relative; left:250; visibility:'visible';" TYPE="button" 
  VALUE="Show My Visibility" onclick="handleClick2()">
and then it will render like this:

Notice that only now the button is 250 pixels to right. Click the button to check its visibility. It will come out empty for Netscape 6 and "visible" for Internet Explorer.