2

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 ?

1

2 Answers 2

3

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);
};
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.