views:

36

answers:

2

Hello,

I have a javascript like

function getCursorPosition(e) {
        e = e || window.event;
        var cursor = {x:0, y:0};
        if (e.pageX || e.pageY) {
            cursor.x = e.pageX;
            cursor.y = e.pageY;
        } 
        else {
            cursor.x = e.clientX + 
                (document.documentElement.scrollLeft || 
                document.body.scrollLeft) - 
                document.documentElement.clientLeft;
            cursor.y = e.clientY + 
                (document.documentElement.scrollTop || 
                document.body.scrollTop) - 
                document.documentElement.clientTop;
        }
        return cursor;
    }

document.onmouseup = function(e){
    cursor = getCursorPosition();
    alert(cursor.x + ':' + cursor.y);
};

this code alerts the X and Y position where the cursor is clicked. This works good in IE 7/8, Chrome/Safari, Opera 10 . But on testing with firefox 4.0 beta 1, it is not working.

On googling, many websites gave me the same code. But it is not working in ff 4.0b

Is this a bug with ff 4.0b ? or can anyone suggest me another cross browser cursor position script ?

+2  A: 

You should pass the event to the getCursorPosition method:

document.onmouseup = function(e){
    cursor = getCursorPosition(e); //<== added the "e" argument here
    alert(cursor.x + ':' + cursor.y);
};
fencliff
+1  A: 

Or lose getCursorPosition() completely and use extremely cross-browser jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
    function jQueryMain ()
    {
        $(document).mouseup (function (evt) {alert (evt.pageX + ':' + evt.pageY);} );
    }

    $(document).ready (jQueryMain);
</script>
Brock Adams