Find out if a specific event is trusted:
<!DOCTYPE html> <html> <body> <button id="myBtn" onclick="myFunction(event)">Test</button> <button onclick="triggerClick()">Test</button> <p id="demo"></p> <script> function myFunction(event) {//from ww w .j a va 2 s .com if ("isTrusted" in event) { if (event.isTrusted) { document.getElementById("demo").innerHTML = "The " + event.type + " event is trusted."; } else { document.getElementById("demo").innerHTML = "The " + event.type + " event is not trusted."; } } else { document.getElementById("demo").innerHTML = "The isTrusted property is not supported by your browser"; } } function triggerClick() { var btn = document.getElementById("myBtn"); btn.click(); } </script> </body> </html>
The isTrusted event property returns a Boolean value indicating whether the event is trusted or not.
In Chrome, Firefox and Opera, the event is trusted if it is invoked by the user.
It not trusted if it is invoked by a script.
In IE, all events are trusted except those that are created with the createEvent()
method.
The isTrusted event property returns a Boolean indicating whether the event is trusted or not.
The isTrusted event property possible values: