javascript - Get click position on the window -
i trying position of click when user click whatever part of window. found this code in many tutorials seems not work.
(function( $ ) { $( document ).ready(function() { $( window ).click(function( e ) { var offset = $(this).offset(), relativex = (e.pagex - offset.left), relativey = (e.pagey - offset.top); alert("x: " + relativex + " y: " + relativey); }); }); })( jquery );
firefox console tells me "typeerror: offset undefined" , don't understand why not work.
which right way retrieve click position on window?
that code close working. work correctly if replace $(this)
$(e.target)
. left , top offsets of click event, not window
itself.
(function($) { $(document).ready(function() { $(window).click(function(e) { var relativex = (e.pagex - $(e.target).offset().left), relativey = (e.pagey - $(e.target).offset().top); alert("x: " + relativex + " y: " + relativey); }); }); })(jquery);