The home where you level up

Save my spot

.isTrusted

Term side of the Card.
Topic: Event
Unit title: .isTrusted
Categories: Property
Explanation side of the Card

Explanation: The browser API property that returns `true` if the event was generated by the browser, for example from a real user click, and `false` if it was created and dispatched by a script with `dispatchEvent()`.

Browser compatibility:
  • The chrome browser supports this feature starting from version 46.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.5.
  • The safari browser supports this feature starting from version 10.
Availability: 

Example

Wrongjs
payButton.addEventListener('click', () => {
  submitPayment();
});
Rightjs
payButton.addEventListener('click', (e) => {
  if (!e.isTrusted) return;
  submitPayment();
});

Ignoring untrusted events blocks synthetic clicks from dispatchEvent(), though page scripts can still call the function directly.

Common mistakes

  • Expecting dispatchEvent(new MouseEvent('click')) to count as a user gesture.

Key points

  • Sensitive actions check event.isTrusted instead of accepting any dispatched event.
  • Tests that need trusted events use browser automation input, since dispatchEvent() events have isTrusted false.
  • Code does not treat isTrusted as bot protection; automation tools can produce trusted input.

Interview questions

Is element.click() a trusted event?

No, element.click() dispatches an event with isTrusted set to false, though it still runs the element's activation behavior.

Can I set isTrusted to true on my own event?

No, isTrusted is read-only, and only the browser sets it to true for real user or browser actions.

The home where you level up

Daily 3-minute lessons to understand your AI's code.

No spam — just launch news.
Unsubscribe anytime. · Privacy Policy

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy