Javascript get mouse position ( compatible IE and Firefox )

following javascript help we get position of mouse event ( compatible IE and Firefox )
var posx = 0;
var posy = 0;
if (!e)
e = window.event;
if (e.pageX || e.pageY){
posx = e.pageX;
posy = e.pageY;
else if (e.clientX || e.clientY){
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
* One note is window.event can not be access direct inside function on FireFox. Solution is pass "event" as parameter when invoke the event. For example :

<div id="xx" onmouseover="DoSomthing(event);">...</div>

within IE, it's ok if we does not provide any parameter as handled in above snippet:

if (!e)
e = window.event;

2 nhận xét:

Cheap cellphone on June 5, 2009 at 2:03 AM said...

i've try this. Work well. thank you

Anonymous said...

yes, works good :) thx


Coding experience share Copyright © 2010 | Designed by Ipietoon for Free Blogger Template