简体   繁体   中英

Is it possible to differentiate between a button click triggered by a mouse click and a click triggered by the user's javascript?

Given a button reference, you can use javascript on your console to .click() it.

Is there a way for my website to differentiate between a click triggered by a mouse and a click triggered by the user's javascript? Or are these effectively equivalent from the browser's point of view?

Use Event.isTrusted

Yes, you can use the event.isTrusted property. Will be true when the user clicks as shown in this code snippet.

 mybutton.addEventListener("click", function(e) { console.log( "isTrusted = " + e.isTrusted ); }); test.addEventListener("click", function(e) { mybutton.click(); });
 <button id="mybutton">My Button</button> <button id="test">Test</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM