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); 

http://jsfiddle.net/ironflare/7wsamt87/


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -