473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Javascript - Getting the element position in IE6

I'm trying to get the position of a table on a page..

<table id="tab">

by using :

var x = getAbsX(document.getElementById("tab"))
var y = getAbsX(document.getElementById("tab"))

which calls:

function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }
function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }

function getAbsPos(elt,which) {
iPos = 0;
while (elt != null) {
iPos += elt["offset" + which];
elt = elt.offsetParent;
}
return iPos;
}

but the thing keeps giving me 0's for both x and y in IE but works in
Firefox. Can anyone help?
Jul 23 '05 #1
9 7003


Bruce Lee wrote:
I'm trying to get the position of a table on a page..

<table id="tab">

by using :

var x = getAbsX(document.getElementById("tab"))
var y = getAbsX(document.getElementById("tab"))

which calls:

function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }
function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top"); }

function getAbsPos(elt,which) {
iPos = 0;
while (elt != null) {
iPos += elt["offset" + which];
elt = elt.offsetParent;
}
return iPos;
}

but the thing keeps giving me 0's for both x and y in IE but works in
Firefox.


When are you calling the functions? Make sure that you call after the
page has been loaded or it is at least ensured the element is rendered.

If you still encounter problems then consider making a small test case
and posting a URL.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Martin

I've put up a test case at
http://www.mycgiserver.com/~directcareer/js/test.html
I need IE6 to correctly recognise the coordinates like Firefox does, I've
tried different methods but no luck so far. Any help would be greatly
appreciated - I've spent far too much time on this!
Jul 23 '05 #3
Bruce Lee wrote:
Martin

I've put up a test case at
http://www.mycgiserver.com/~directcareer/js/test.html
I need IE6 to correctly recognise the coordinates like Firefox does, I've tried different methods but no luck so far. Any help would be greatly
appreciated - I've spent far too much time on this!


Apparently a bit of a 'race condition' there...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
<table height=20><tr><td></td></tr></table>
<table width=500 height=100 border=1>
<tr>
<td width=100 bgcolor=blue>
</td>
<td bgcolor=yellow>
<table width=100% id="tab" width=50 height=20 border=1 bgcolor=red>
<tr>
<td>

<script type="text/javascript">

function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left");
}
function getAbsY(elt) { return (elt.y) ? elt.y : getAbsPos(elt,"Top");
}

function getAbsPos(elt,which) {
iPos = 0;
while (elt != null) {
iPos += elt["offset" + which];
elt = elt.offsetParent;
}
return iPos;
}

onload=function()
{
alert(

'This red table (id:tab) is at - X:'+
getAbsX(document.getElementById("tab"))+
',Y:'+getAbsY(document.getElementById("tab"))

);
}

</script>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

Don't forget doctypes for testing like this; layout will vary with
different ones specified.

Jul 23 '05 #4


Bruce Lee wrote:

I've put up a test case at
http://www.mycgiserver.com/~directcareer/js/test.html
I need IE6 to correctly recognise the coordinates like Firefox does, I've
tried different methods but no luck so far. Any help would be greatly
appreciated - I've spent far too much time on this!


As said, you need to make sure the element is completely rendered, if
you call your functions in window.onload then you get the values you
want but calling them while the table is not yet completely rendered is
not reliable as you have found.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #5
So would you say this is a bug in IE6? - I can't see a way of fixing it
through normal methods.
Jul 23 '05 #6


Bruce Lee wrote:
So would you say this is a bug in IE6?
Not really, if you try to find the position or dimensions of something
the browser has not completely rendered there can be inaccuracies.
I can't see a way of fixing it
through normal methods.


It was already suggested to use your functions when the page has been
loaded, then IE gives you the correct values.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #7
So I have to wait until all the DOM has been built to access elements? At
what point would you say it would safe to access elements? Just before
</html>?
Jul 23 '05 #8


Bruce Lee wrote:
So I have to wait until all the DOM has been built to access elements? At
what point would you say it would safe to access elements? Just before
</html>?


As already suggested, to play safe use
<body onload="...">
respectively
window.onload = function (evt) {
...
}
that way stuff in the page is loaded.

Within text/html an element is usually accessible by script after its
end tag has been parsed but I wouldn't necessarily expect dimension or
position to be reliable or fixed then.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #9
The problem with that is that I need the table generated by the javascript
to be embedded inside a table in the middle of the page??
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: news frontiernet.net | last post by:
I have key entered and tried to run example 4-6 from Dany Goodmans DYNAMIC HTML book, version one that is on pages 94-96. This is part of my effort to learn JavaScript. I checked each byte and...
3
by: Mr. x | last post by:
Hello, I have simple table <table id = "mytable" ....> .... </table> I want to get the left position of the table. Everything I did getting wrong :
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
15
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
4
by: bboyle18 | last post by:
Hi, I am working with a table sorting script which can be found here http://www.workingwith.me.uk/articles/scripting/standardista_table_sorting This script works very nicely, but when there is a...
16
by: Tony Girgenti | last post by:
Hello. I just finished reading two articles about javascript here: http://msdn2.microsoft.com/en-us/library/bb332123(VS.80).aspx I tried some javascript testing in VS2005, SP1, VB , .NET 2.0,...
15
by: javelin | last post by:
I need to be able to create a javascript based drawing/signature box, and be able to save it. Can someone refer me to a script that will allow this, or even a 3rd party package for sale? It can't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.