473,378 Members | 1,699 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,378 software developers and data experts.

How to get X,Y coord's like "ismap" ?

For use in IE only:

I need to make some calculations (in a client-side script) based on
the X,Y coordinates of the mouse pointer when the user clicks on an
image. I want the coordinates to be screen-resolution independent.

The image tag's "ismap" generates exactly the numbers I'm looking for
but I can't figure out how to capture them. "ismap" seems to work only
when used in conjunction with an "href" and a new page is
automatically requested. Is there a way to grab those numbers without
requesting a new page?

Alternatively, can someone tell me how to generate the equivalent
numbers?

I've tried looking at just about every X and Y related property on the
"image" and "event" objects but I can't seem to duplicate the "ismap"
numbers.

Any thoughts?

Thanks
Jul 23 '05 #1
9 3491
Martin wrote on 21 aug 2004 in comp.lang.javascript:
For use in IE only:

I need to make some calculations (in a client-side script) based on
the X,Y coordinates of the mouse pointer when the user clicks on an
image. I want the coordinates to be screen-resolution independent.

The image tag's "ismap" generates exactly the numbers I'm looking for
but I can't figure out how to capture them. "ismap" seems to work only
when used in conjunction with an "href" and a new page is
automatically requested. Is there a way to grab those numbers without
requesting a new page?

Alternatively, can someone tell me how to generate the equivalent
numbers?

I've tried looking at just about every X and Y related property on the
"image" and "event" objects but I can't seem to duplicate the "ismap"
numbers.


Googling gives many examples:

<html>
<script type="text/JavaScript">
document.onmousemove = doit;
function doit() {
X = event.x + document.body.scrollLeft - 2;
Y = event.y + document.body.scrollTop - 2;
window.status=
"Coordinates of the pointer: x = " + X + " ; y = " + Y;
}
</script>
Mouse coordinates are shown on the statusbar.
</html>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #2
On 21 Aug 2004 22:13:09 GMT, "Evertjan."
<ex**************@interxnl.net> wrote:
Googling gives many examples:

<html>
<script type="text/JavaScript">
document.onmousemove = doit;
function doit() {
X = event.x + document.body.scrollLeft - 2;
Y = event.y + document.body.scrollTop - 2;
window.status=
"Coordinates of the pointer: x = " + X + " ; y = " + Y;
}
</script>
Mouse coordinates are shown on the statusbar.
</html>


Thanks for the reply. In fact, I'm already using that code for some
other purposes.

But it does not yield the numbers I'm looking for; it returns the
mouse pointer's screen coordinates. And, the actual numbers returned
are dependent on the screen resolution.

I'm looking for something that tells me where the pointer is <in the
image> AND, will return the same values no matter what the display
resolution is.

Any other thoughts?
Jul 23 '05 #3
On Sat, 21 Aug 2004 15:36:01 -0700, Martin <ma**********@comcast.net>
wrote:
On 21 Aug 2004 22:13:09 GMT, "Evertjan."
<ex**************@interxnl.net> wrote:
Googling gives many examples:

<html>
<script type="text/JavaScript">
document.onmousemove = doit;
function doit() {
X = event.x + document.body.scrollLeft - 2;
Y = event.y + document.body.scrollTop - 2;
window.status=
"Coordinates of the pointer: x = " + X + " ; y = " + Y;
}
</script>
Mouse coordinates are shown on the statusbar.
</html>


Thanks for the reply. In fact, I'm already using that code for some
other purposes.

But it does not yield the numbers I'm looking for; it returns the
mouse pointer's screen coordinates. And, the actual numbers returned
are dependent on the screen resolution.

I'm looking for something that tells me where the pointer is <in the
image> AND, will return the same values no matter what the display
resolution is.

Any other thoughts?


Oops...

Pardon me for a moment while I pull my head out of my ass. :(

I went back and took another look at function I had (that you
provided) and found that it does, in fact, provide the same numbers at
differing resolutions. What I was failing to do was to properly
account for the offsets (to the centered image) that are generated at
different resolutions. When I calculate things correctly, I get the
numbers I wanted.
Jul 23 '05 #4
On Sat, 21 Aug 2004 16:07:40 -0700, Martin <ma**********@comcast.net>
wrote:
On Sat, 21 Aug 2004 15:36:01 -0700, Martin <ma**********@comcast.net>
wrote:
On 21 Aug 2004 22:13:09 GMT, "Evertjan."
<ex**************@interxnl.net> wrote:
Googling gives many examples:

<html>
<script type="text/JavaScript">
document.onmousemove = doit;
function doit() {
X = event.x + document.body.scrollLeft - 2;
Y = event.y + document.body.scrollTop - 2;
window.status=
"Coordinates of the pointer: x = " + X + " ; y = " + Y;
}
</script>
Mouse coordinates are shown on the statusbar.
</html>


Thanks for the reply. In fact, I'm already using that code for some
other purposes.

But it does not yield the numbers I'm looking for; it returns the
mouse pointer's screen coordinates. And, the actual numbers returned
are dependent on the screen resolution.

I'm looking for something that tells me where the pointer is <in the
image> AND, will return the same values no matter what the display
resolution is.

Any other thoughts?


Oops...

Pardon me for a moment while I pull my head out of my ass. :(

I went back and took another look at function I had (that you
provided) and found that it does, in fact, provide the same numbers at
differing resolutions. What I was failing to do was to properly
account for the offsets (to the centered image) that are generated at
different resolutions. When I calculate things correctly, I get the
numbers I wanted.

Hi there,

I'm very interested in how to do this as well. I have a "map" image
and I want the users to click on the image and depending on the X,Y
location they clicked on I want to have a popup that access the DB
with the X.Y location and return the info. (the db has X,Y locations
stored and will compare the ones passed to the ones stored.

At the moment the only method I have thought of is by using a form.
function CityInfoWindow() {
NewWindow ('', 'CityInfo', 600, 400, 'yes', 'yes', -1, -1,
'');

<form name="frmCity" method="post" action="CityInfo.asp"
target="CityInfo">
<input type="image" src="CityMap.gif" name="coords"
onClick="CityInfoWindow();">
</form>

I need the top left of the Image to be starting at 0,0 and for the
Javascript code to work in both IE 5.5+ and NS 7 at least.
Thanks for any help

Al.

Jul 23 '05 #5
On Sun, 22 Aug 2004 12:00:39 +0100, Harag
<ha**********************@softhome.net> wrote:
Hi there,

I'm very interested in how to do this as well. I have a "map" image
and I want the users to click on the image and depending on the X,Y
location they clicked on I want to have a popup that access the DB
with the X.Y location and return the info. (the db has X,Y locations
stored and will compare the ones passed to the ones stored.

At the moment the only method I have thought of is by using a form.
function CityInfoWindow() {
NewWindow ('', 'CityInfo', 600, 400, 'yes', 'yes', -1, -1,
'');

<form name="frmCity" method="post" action="CityInfo.asp"
target="CityInfo">
<input type="image" src="CityMap.gif" name="coords"
onClick="CityInfoWindow();">
</form>

I need the top left of the Image to be starting at 0,0 and for the
Javascript code to work in both IE 5.5+ and NS 7 at least.
Thanks for any help

Al.


Here's the code I ended up using to generate the x,y coordinates:

x=event.clientX + window.document.body.scrollLeft -
document.images[0].offsetLeft - 2;

y=event.clientY + window.document.body.scrollTop -
document.images[0].offsetTop- 2;

I'm writing strictly for the IE browser. I know you'll have to use
different code to capture the "event.clientX" and "event.clientY"
values in NS.

Good luck.

Jul 23 '05 #6
On Sun, 22 Aug 2004 12:00:39 +0100, Harag
<ha**********************@softhome.net> wrote:

Thanks for any help

Al.


I've been working on this some more and have discovered that the
".offsetTop" property seems to always be "0" even though the image is
not at the top of the page. Apparently, the offset is measured from
the top of the "parent" object. In my case, I have the image in a
<DIV> tag so, I had to get that value. I don't know if it's the best
way but instead of "document.images[0].offsetTop", I used
"document.images[0].offsetParent.offsetTop"

This seems to yield a constant value - even at different screen
resolutions, it's always the same number.

HTH

Jul 23 '05 #7
Martin wrote on 22 aug 2004 in comp.lang.javascript:
I've been working on this some more and have discovered that the
".offsetTop" property seems to always be "0" even though the image is
not at the top of the page. Apparently, the offset is measured from
the top of the "parent" object. In my case, I have the image in a
<DIV> tag so, I had to get that value. I don't know if it's the best
way but instead of "document.images[0].offsetTop", I used
"document.images[0].offsetParent.offsetTop"

This seems to yield a constant value - even at different screen
resolutions, it's always the same number.


offsetTop belongs to a style="position:absolute;" object

Do not use offsetTop with a object [like <div>]
that is not absolute positioned in it's container.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 23 '05 #8
Martin wrote:
<snip>
Here's the code I ended up using to generate the x,y coordinates:

x=event.clientX + window.document.body.scrollLeft -
document.images[0].offsetLeft - 2;

y=event.clientY + window.document.body.scrollTop -
document.images[0].offsetTop- 2;

I'm writing strictly for the IE browser. I know you'll have to use
different code to capture the "event.clientX" and "event.clientY"
values in NS.


IE browser mouse event objects have - offsetX/Y - properties that may be
a more direct way of achieving your goal (they are not well supported on
other browsers).

However, An element's total offset into a page is more complex than
just - document.images[0].offsetTop - because that represents the offset
to the element's - offsetParent -. When the element is not a direct
child of the BODY its - offsetTop/Left - may not be its page offset (CSS
absolute positioning also influences offset values).

Position information is available (where supported, as not all browsers
provide this info) by summing the - offsetTop/Left - values for each
element with all of its offset parent elements - offsetTop/Left - values
and then its offset parent's, until one of those elements has its -
offsetParent - property set to - null -. At least that is in the
simplest case; element's with borders and CSS overflow set so that they
scroll, add additional complexity to the calculations).

The constant value that you are using - 2 - is compensating for the -
clientTop/Left - property of the document.body element (on IE <= 5.5 and
IE 6 in "BackCompat" mode) and represents a default 2px inset border on
the root element. In principle that number is only the default and it is
possible for users to modify the border width on the root element (or
even a page designer through a CSS assignment). It would probably be
better to actually read the - document.body.clientTop/Left - property to
acquire that number as then it will always represent the user's real
configuration.

On IE 6 it is possible to put the browser into "standards"
('CSS1Compat') mode, and if that happens the - scrollTop/Left - values
and the - clientTop/Left - values that apply to the root element are not
available from - document.body - but should instead be read from -
document.documentElement -. If there is a chance that the script will be
used in more than one context, or that the HTML design may at some point
change to be formally valid, it would be a good idea to be deciding
which of - document.body - or - document.documentElement - should be
read by checking the string value available from - docuemnt.compatMode -
(which doesn't exist on IE <= 5.5). Archive searching for
"document.compatMode" will turn up many examples and approaches.

Richard.
Jul 23 '05 #9
On Sun, 22 Aug 2004 19:53:07 +0100, "Richard Cornford"
<Ri*****@litotes.demon.co.uk> wrote:

snip...

Richard - Thanks for the explanation. I've learned a lot.

One thing I've learned, though, is that this is becoming more
complicated than it's worth (for my particular application).

As I mentioned at the top of this thread (I think), the "ismap"
function of an image gives me exactly the x/y coordinates that I'm
looking for. Do you (or anyone else) know if there's any way to
capture those values inside a script? They show up in the status
window only if an associated "href" has been specified but I'm
wondering if they're being generated by just declaring "ismap".

Any thoughts?

Thanks again.
Jul 23 '05 #10

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

Similar topics

1
by: Scott M | last post by:
Hi, I am writing a small game that is using a form 800 * 600. The form can be scrolled vertically up to a height of 1200 (basically the form can be scrolled down one screen vertically). The...
1
by: Dave Harris | last post by:
I have written a reasonably clean way to perform coordinate transformations on a polygon, but Tkinter Canvas is not being particularly cooperative. The following program has been distilled down to...
1
by: Vangelis Natsios | last post by:
I want to create a page with a scrolling image that will cause different messages to appear on another part of the page (say, another <div>) as the image will scroll. Imagine something like this: ...
2
by: Stimp | last post by:
I remember doing something like this before, but it's been a while since I've touched javascript... I have a standard <img> and when a user clicks the image I want to output the IMAGE x y...
1
by: Antonio Orozco | last post by:
Ok, here is the deal. I want to do this.... i have a group of cubes, i want to draw them, and then rotate, etc,etc. i've already tried DirectX 9 but now i want to draw the images direct to a...
3
by: tommydog | last post by:
i am trying to generate an image map from the mouse coords of a click. i can pass the X Y through ok as $_POST $_POST how should the string be put into the AREA tag ? i am keeping the diameter...
3
by: Matthias Vodel | last post by:
Hi all, I want to change the beginning/end-coordinates of a canvas.line item. Something like: self.myCanvas.itemconfigure(item_id, coords=(x1_new, y1_new, x2_new, y2_new)) I don't want...
0
by: Head In A Pan | last post by:
Hello! I have a basic javascript mouse tracker which captures the X&Y coords of the window & displays them in two separate text fields. What I am desperate to do is to post these values to...
1
by: jojoba | last post by:
Hi I am trying to determine coordinates of something in a webpage. All i have is a subset of html that i know exists in a given webpage. Now i want to find that subsetHtml in the page and...
3
by: Rhishabh07 | last post by:
how to define coords in an image for an area shape i m using <img src="left.jpg" width="350" height="29" alt="Planets" usemap ="#planetmap" /> <map id ="planetmap" name="planetmap">...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.