Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does the browser tell the difference between user click and programmatic click?

If I initiate a click with jquery which does window.open(), it is blocked by pop up blocker, If I do the click manually the window is not blocked. How does the browser differ between the two?

like image 704
anon Avatar asked Jun 19 '12 23:06

anon


2 Answers

The Firefox implementation of this is discussed at length on this bug, and this other bug has some further interesting background, including what lengths sites will go through in order to foist an unwanted popup window on unsuspecting users (in this case: calling window.open() from within an image load event). If you search around Bugzilla you'll find that the Mozilla people took a number of years to get this all working correctly, for example here's a bug from 2001.

The way it works currently is this: When Firefox receives a click event from the operating system, for a certain amount of time window.open() is enabled in JavaScript (look for dom.disable_open_click_delay in about:config). If you call the click() event from code without a user click occurring then the first step, the enabling of window.open() never occurs, though the call to window.open() will itself succeed to stop sites detecting that you have popup blocking enabled.

I'm not sure how other browsers implement this stuff but it would be surprising to me if it was much different.

like image 63
robertc Avatar answered Nov 05 '22 13:11

robertc


I believe that calling click via jQuery actually doesn't really trigger a click on the element, but instead calls the function listening to the click. So although you are calling click() you are actually just calling a function.

When the user clicks, it is a real click.

Example:

var handler = function () {
    alert('hi');
};

$('#example').on('click', handler);
$('#example').click(); // really just calls handler();
like image 1
Fenton Avatar answered Nov 05 '22 12:11

Fenton