31

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:

  1. 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(); / } 
  2. if((inputElem !== _focussedInput)) setTimeout(function(){ inputElem.focus(); },10);

and many more...

6
  • 1
    jquery is just javascript. If in jquery you can do it, you can do it in js. Commented Apr 22, 2015 at 12:50
  • hmmm....any idea how do they do it in jQuery? Commented Apr 22, 2015 at 12:50
  • What code is programmatically triggering the event? Commented Apr 22, 2015 at 12:51
  • event.isTrusted - but only partial browser support Commented Apr 22, 2015 at 12:58
  • “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 that originalPrevent 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. Commented Apr 22, 2015 at 12:58

4 Answers 4

23

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)

Sign up to request clarification or add additional context in comments.

3 Comments

This is not currect. Can be trusted even if triggered by code.
At 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 a user-gesture. But maybe that's just a bug or a user-gesture is something more complex than just 'triggered-by-user'.
15

[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

4 Comments

hmm...let me give it a shot. Meanwhile can you please explain me how does it work?
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.
@Vijay Of all the solutions I tried, e.isTrusted or e.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.
Does not work with focus event even if triggered by a mouse click
4

I know how to do it in jQuery

you can use the event object by checking e.isTrigger

Fiddle

$(".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>

3 Comments

Thanks 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 This answer helps those who may have the same question as you but can use jQuery.
To add on above, one can check jquery source codes and check for implementation and repeat it in his custom javascript.
0

This working for me

if ((event.originalEvent.isTrusted === true && event.originalEvent.isPrimary === undefined) || event.originalEvent.isPrimary === true) { //Hey hooman it is you //Real CLick } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.