By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

Screen coordinates of object

P: 1
Hi everybody!

I need a script, which calculates screen coordinates of given object (e.g. textbox or div). By "screen coordinates" I mean offset from upper left corner of the screen (not frame or browser window).

Thanks in advance.
Oct 4 '06 #1
Share this Question
Share on Google+
11 Replies


acoder
Expert Mod 15k+
P: 16,027
Use the screenX/screenY properties.
May 20 '08 #2

hsriat
Expert 100+
P: 1,654
You won't be able to get that if the HTML is rendered itself. But if you have set the position, and left and top of the elements by CSS (or javascript), only then you would be able to retrieve the x and y coordinates as
Expand|Select|Wrap|Line Numbers
  1. var x = parseInt(elementObj.style.left); 
  2. var y = parseInt(elementObj.style.top);
May 21 '08 #3

hsriat
Expert 100+
P: 1,654
Use the screenX/screenY properties.
And that was a really old post! ...:D
May 21 '08 #4

Markus
Expert 5K+
P: 6,050
And that was a really old post! ...:D
Must've been bored.

Like me
May 21 '08 #5

acoder
Expert Mod 15k+
P: 16,027
You won't be able to get that if the HTML is rendered itself. But if you have set the position, and left and top of the elements by CSS (or javascript), only then you would be able to retrieve the x and y coordinates as
Expand|Select|Wrap|Line Numbers
  1. var x = parseInt(elementObj.style.left); 
  2. var y = parseInt(elementObj.style.top);
That would give you the x/y coordinates within the browser, not the screen

...and yes, it's an old post, but no-one answered it (poor thing!)
May 21 '08 #6

hsriat
Expert 100+
P: 1,654
Must've been bored.

Like me
But you don't reply in your own threads... :-/
May 21 '08 #7

hsriat
Expert 100+
P: 1,654
That would give you the x/y coordinates within the browser, not the screen
oh.. I see... good point..
So one can still calculate the browser's offset wrt the screen by doing
Expand|Select|Wrap|Line Numbers
  1. var offsetX = event.screenX - event.pageX; //(or clientX)
  2. var offsetY = event.screenY - event.pageY; //(or clientY)
And then add that in x and y calculated above...
what say? :p

...and yes, it's an old post, but no-one answered it (poor thing!)
I hadn't joined by the time. :p
May 21 '08 #8

acoder
Expert Mod 15k+
P: 16,027
oh.. I see... good point..
So one can still calculate the browser's offset wrt the screen by doing
Expand|Select|Wrap|Line Numbers
  1. var offsetX = event.screenX - event.pageX; //(or clientX)
  2. var offsetY = event.screenY - event.pageY; //(or clientY)
And then add that in x and y calculated above...
what say? :p
screenX and screenY should be enough, see link.
I hadn't joined by the time. :p
Neither had I ;)
May 21 '08 #9

hsriat
Expert 100+
P: 1,654
screenX and screenY should be enough, see link.
But still, how would you tell what are the coordinates of a <div> element (for instance) wrt the screen? I'm not getting what you are trying to say.

This is what I understand from screenX and pageX:

May 21 '08 #10

acoder
Expert Mod 15k+
P: 16,027
But still, how would you tell what are the coordinates of a <div> element (for instance) wrt the screen? I'm not getting what you are trying to say.
I see now what you mean. Yes, screenX/Y would give the screen co-ordinates, pageX/Y the co-ordinates relative to the document. However, it's not supported in IE, so you'll have to use clientX/Y plus scrolling offset in IE to get the same value.
May 21 '08 #11

hsriat
Expert 100+
P: 1,654
I see now what you mean. Yes, screenX/Y would give the screen co-ordinates, pageX/Y the co-ordinates relative to the document. However, it's not supported in IE, so you'll have to use clientX/Y plus scrolling offset in IE to get the same value.

I know its not an IE thing. But while having a logical conversation, we should keep IE out. :D
Of course it would need some fix as usual. :)
May 21 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.