Detect Whether an Event is Triggered by User and not programmatically
Solution 1
In latest browsers, Event.isTrusted is available for this particular use-case. As per its MDN document:
The isTrusted read-only property of the Event interface is a boolean that is true when the event was generated by a user action, and false when the event was created or modified by a script or dispatched via dispatchEvent.
You can simply check inside any event handler:
if (e.isTrusted) {
/* The event is trusted. */
} else {
/* The event is not trusted. */
}
Browser Support: Chrome 46.0, Firefox (latest), Opera 33, Safari & IE (no support)
Solution 2
[Workaround] Javascript: Check if event.screenX
& event.screenY
are non-zero.
var button = document.getElementsByTagName('button')[0];
button.onclick = function(e) {
if(e.screenX && e.screenX != 0 && e.screenY && e.screenY != 0){
alert("real button click");
}
}
When the user clicks on button, it would have some real screenX and screenY based on the button position. But when you do it from code - it would be zero
Solution 3
I know how to do it in jQuery
you can use the event object by checking e.isTrigger
$(".lol").click(function(e){
console.log(e)
alert("Is triggered: " + (e.isTrigger ? true: false))
})
$(".trigger-lol").click(function(e){
$(".lol").trigger("click")
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="lol">lol</div>
<div class="trigger-lol">Trigger lol</div>
Related videos on Youtube
bhavya_w
Updated on July 28, 2022Comments
-
bhavya_w over 1 year
Well this question has been asked before but in context of jQuery. In jQuery we can check it by originalEvent property of event Object (link) which tells whether its a manual or programmed event.
In my case I am using Javascript Event listeners and triggers. Can we differentiate between the two kind of events (programmed and manual) in this case?
If not then any workarounds?
My Listeners:
function setUpListeners(){ _cellViewWrapper.addEventListener('mousedown',mouseDownHandler,false); _cellViewWrapper.addEventListener('mouseover',mouseEnter,false); _cellViewWrapper.addEventListener('blur',blurHandler,true); _cellViewWrapper.addEventListener('focus',focusEventHandler,true); }`
Trigger use Cases:
if(!IE_FLAG) hidePicker(); //if browser is internet explorer else{ //if blur is allowed then hide Picker if(_ieBlurAllowed) hidePicker(); //if blur is not allowed -- keep focus on picker input //triggering the focus event here else blurredElement.focus(); / }
if((inputElem !== _focussedInput)) setTimeout(function(){ inputElem.focus(); },10);
and many more...
-
kemicofa ghost almost 9 yearsjquery is just javascript. If in jquery you can do it, you can do it in js.
-
bhavya_w almost 9 yearshmmm....any idea how do they do it in jQuery?
-
Scimonster almost 9 yearsWhat code is programmatically triggering the event?
-
James Thorpe almost 9 years
event.isTrusted
- but only partial browser support -
CBroe almost 9 years“In jQuery event object has originalEvent property via which we can tell whether its a manual or programmed event” – I doubt that. Documentation only says that certain events may have additional properties than the one the jQuery
Event
object provides, and thatoriginalPrevent
can be used to access those, as it refers to the native JS Event object. But I don’t see how that would allow to differentiate between an event triggered by the user and one triggered via script. -
bhavya_w almost 9 years@CBroe . You are right. Edited the Question.
-
bhavya_w almost 9 yearsThanks for the reply.Like i told in my Question I am using pure Js event and triggers. Had i been using jQuery I would'nt have posted this question.
-
bhavya_w almost 9 yearshmm...let me give it a shot. Meanwhile can you please explain me how does it work?
-
Vijay almost 9 yearsWhen the user clicks on button, it would have some real screenX and screenY based on the button position. But when you do it from code - it would be zero.
-
Anurag over 7 years@Vijay Of all the solutions I tried,
e.isTrusted
ore.originalEvent
or 'e.which' which all fail when we fire a DOM click like$("#button")[0].click();
Your's is the only solution which works across all scenarios and combination of clicks. But unfortunately it doesn't work for keyboard clicks. But anyways +1 for a clean and simple solution. -
reformed about 6 years@bhavya_w This answer helps those who may have the same question as you but can use jQuery.
-
Nadav over 2 yearsThis is not currect. Can be trusted even if triggered by code.
-
JepZ over 2 yearsAt least with the AmazonKids browser (based on Chrome 94), there are events (e.g. touchstart) that have
isTrusted === true
but are unable to start audio.play() because they are not initiated by auser-gesture
. But maybe that's just a bug or a user-gesture is something more complex than just 'triggered-by-user'.