views:

43

answers:

2

I have this simple script that catches all mouse clicks, unless you click on something that actually works. Links, flash videos, etc. How can I adjust this so no matter what a user clicks on, before the video loads, new page loads, etc. It sends the simple GET request I built?

(function($) { 

$.fn.saveClicks = function() { 
$(this).bind('mousedown.clickmap', function(evt) {
    var clickDocument = (document.documentElement != undefined && document.documentElement.clientHeight != 0) ? document.documentElement : document.body;
    var width = clickHeatDocument.clientWidth != undefined ? clickDocument.clientWidth : window.innerWidth;
    var height = clickHeatDocument.clientHeight != undefined ? clickDocument.clientHeight : window.innerHeight;
    var scrollx = window.pageXOffset == undefined ? clickDocument.scrollLeft : window.pageXOffset;
    var scrolly = window.pageYOffset == undefined ? clickDocument.scrollTop : window.pageYOffset;
    var x = evt.clientX + scrollx;
    var y = evt.clientY + scrolly;
    $.get('/click-save.php', {  
        "x":x,  
        "y":y,
        "click":"true",
        "w":width,
        "h":height,
        "l":escape(document.location.pathname),
        "d":escape(document.domain)
    }); 
}); 
};

})(jQuery); 

$(function() {
    $(document).saveClicks();
});
+1  A: 
Sam Nicholson
+1  A: 

I think the best would be to use live binding and the * selector

$('*').live('click.clickmap', function(evt){ ... });

also the ajax call might need to be synchronous ( you would need to use .ajax() for this.. ) (not sure if this is required though, to avoid the get call being interupted by a normal link click)

Gaby
I was just using get() because I don't need a response from the server. I tried your example, and it seems to have worked great!
Hallik
@Hallik, normally `get` is used to **get** a response from the server.. use `post` if you want to make an action that alters something.. see [When do you use POST and when GET](http://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-use-get)
Gaby
@Gaby, but doesn't POST make two calls, and GET makes just one? I was just trying to make as little calls as possible, and I thought GET would be lighter weight.
Hallik
@Hallik, indeed that is true for AJAX calls, but if you go for semantics it all depends on what action you are trying to perform .. in the end it is all up to you :) ..
Gaby