Forum Moderators: open

Message Too Old, No Replies

Can't get screen coordinates with Safari

Safari gives really weird results

         

MichaelBluejay

8:05 am on Mar 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm trying to find the mouse coordinates relative to the screen (rather than relative to the window, or to the page). That's because I'm trying to popup a small info window when the user clicks a link and position the window over the link, but I want to make sure I don't position any part of the window outside the bounds of the monitor.

The following code shows the x,y coordinates of the mouse in the status bar. It works fine in IE, but I have two problems in Safari:

(1) The X coordinate is relative to the window, not the screen.

(2) The Y coordinate is inverted! That is, 0 is on the BOTTOM of the window and gets higher as you move the mouse UP.

Here's my code. I spent over an hour scouring the search engines but turned up nothing. Any ideas?

Thanks for your help, -MBJ-

-------------------------


var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
if (IE) {
tempX = event.screenX
tempY = event.screenY
}
else {
tempX = e.screenX;
tempY = e.screenY;
}

if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}
window.status = tempX+","+tempY;
return true;
}

Rambo Tribble

3:31 pm on Mar 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see that getMouseXY() is passed a variable e, but where does e originate and what is its value?

dcrombie

3:38 pm on Mar 20, 2004 (gmt 0)



The e is passed automatically by the event to your event handler. This is how things are done in Netscape-land. In IE they just make everything global (obviously they never taught OOP in Redmond ;)).

If you find a bug in Safari you should report it to Apple using the link in the Safari menu - then stay tuned to [weblogs.mozillazine.org...] for updates.

Rambo Tribble

4:33 pm on Mar 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the clarification, dcrombie.

Now, be fair, Redmond may not know OOP, but they sure know, "Oops!"

Oh, and Safari is based on Konquerer (or however it's spelled) so you might try the KDE site for clarification.