Check If Event Is Triggered by a Human

Check if event is triggered by a human

You can check e.originalEvent: if it's defined the click is human:

Look at the fiddle http://jsfiddle.net/Uf8Wv/

$('.checkbox').change(function(e){
if (e.originalEvent !== undefined)
{
alert ('human');
}
});

my example in the fiddle:

<input type='checkbox' id='try' >try
<button id='click'>Click</button>

$("#try").click(function(event) {
if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}

});

$('#click').click(function(event) {
$("#try").click();
});

Event triggered by human or jQuery

try with trigger() , however i have no idea why aren't you using $("#try").click(); and why it won't work with you.

$("#try").click(function (event) {
if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});

$('#click').click(function (event) {
// $("#try").click();
$("#try").trigger('click');
});

fiddle here

How to check if the focus event is triggered programmatically

Your problem is that the focus event doesn't bubble.

jQuery fixes that with a little magic to make it more like the other events, but it still doesn't quite work like an event that naturally bubbles.

To fix the problem, use the focusin event instead, as it bubbles, and do .trigger('focusin')

jQuery(document).on('focusin', '#song_artist_focus', function(event) {  if (event.originalEvent === undefined) {    console.log('I am not human');  } else {    console.log('I am human');  }});

jQuery('#song_artist_focus').trigger('focusin');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input id="song_artist_focus">

Detect synthetic clicks on a webpage

You can use vanilla Javascript as in the other anwsers or consider using jquery so you can easily detect non-human clicks.

1.Define a global click event handler using jquery.So you can catch all the clicks on page elements.

 $(document).click(function(event) {
});

2.Check if the event.originalEvent is exists if it exists click was performed by a human.
More info

For mouse clicks

 $(document).click(function(event) {
if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});

For keypress

$(document).keypress(function(event) {

if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});

Here is a sample code.

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).click(function(event) {
if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});
$(document).keypress(function(event) {

if (event.originalEvent === undefined) {
alert('not human')
} else {
alert(' human');
}
});
$(document).click();
$(document).keypress();
});
</script>
</head>
<body>
<p>Click me !</p>
</body>
</html>


Related Topics



Leave a reply



Submit