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
Which Characters Are Valid/Invalid in a JSON Key Name
How to Inherit from a Class in JavaScript
How to Match Multiple Occurrences with a Regex in JavaScript Similar to PHP's Preg_Match_All()
Detect Multiple Keys on Single Keypress Event in Jquery
Why Use Jquery On() Instead of Click()
Uploading Image to Amazon S3 with HTML, JavaScript & Jquery with Ajax Request (No PHP)
How to Tell If a Browser Supports <Input Type='Date'>
Selecting Multiple from an HTML Select Element Without Using Ctrl Key
How to Load Images to User's Cache Asynchronously
Replace a Regex Capture Group with Uppercase in JavaScript
Write/Add Data in JSON File Using Node.Js
Correct Use of Arrow Functions in React
Is This Simple String Considered Valid JSON
Ecmascript 6 Features Available in Node.Js 0.12
Internet Explorer 11 Detection