TomSawyer
TomSawyer

Reputation: 3820

Track click on whole HTML page with JQUERY

i'm trying to track click on html element.

with:

$('html').click(function() {
    // action here
});

but this thing won't work with iframe , coudn't catch iframe click, or click to selectionbox

http://jsfiddle.net/TYJtc/4/

Upvotes: 0

Views: 1956

Answers (3)

Huangism
Huangism

Reputation: 16448

This answer is based on the iframe content is of your domain, using jquery 1.7.2 and this should be inside of document ready

http://jsfiddle.net/TYJtc/6/

html

<iframe id="myframe" src='/'></iframe>

js

$('#myframe').contents().find('body').bind('click', function(e) {

   alert('clicked');
});

Upvotes: 3

Gaurav Shetty
Gaurav Shetty

Reputation: 461

You could place a div with a high z-index over the iframe. That should intercept all clicks and call the document click handler. The major issue is just that though. It intercepts all clicks so the underlying iframe will not receive any click events.

Upvotes: 0

Dan
Dan

Reputation: 526

You need to place your script inside the document.ready function, and change 'html' to document.

$(document).ready(function() {
    $(document).click(function(e) {
     alert("click");
    });
});

http://jsfiddle.net/TYJtc/1/

Upvotes: 1

Related Questions