2

I want to be able to simulate an isTrusted=true when I call a touchStart event. Is there any library or workaround of any sort to make this possible?

Here is the output when I run the touchStart programmatically vs. when I actually call the touchStart.

enter image description here

I am using mobile safari. According to this site mobile safari does not support it but that can't be true as the output shows the presence of the event. Any help/advice on this would be greatly appreciated. Thank you.

MGames
  • 1,171
  • 2
  • 13
  • 24
  • Simulate to whom? Of course you can make an object that looks like a trusted event to an event listener, but no you cannot make a browser trust arbitrary events. Also, what is your [actual problem](https://meta.stackexchange.com/q/66377)? – Bergi Apr 28 '18 at 10:38

1 Answers1

1

Not possible. Events triggered through scripts will always be marked as not trusted (unless using IE). This is for security reasons. In other words event.isTrusted === false if you invoke it through script.

For more info read this.

Pretty sure you are already getting these results but here is an example with clicks. Notice how the first console message is false which is the one that is called by the script:

const element = document.querySelector('div');
const scriptEvent = new Event('click');

element.addEventListener('click', event => console.log(event.isTrusted));

element.dispatchEvent(scriptEvent);
<div>CLICK HERE</div>
Cory Kleiser
  • 1,969
  • 2
  • 13
  • 26