Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{Element,Document}: securitypolicyviolation event: wrong claim about "you can't assign a handler to the property until the elements have been loaded" #35410

Open
Josh-Cena opened this issue Aug 12, 2024 · 0 comments · May be fixed by #38685
Labels
Content:WebAPI Web API docs

Comments

@Josh-Cena
Copy link
Member

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/Element/securitypolicyviolation_event

What specific section or headline is this issue about?

No response

What information was incorrect, unhelpful, or incomplete?

You should add the handler for this event to a top level object (i.e. {{domxref("Window")}} or {{domxref("Document")}}).
While the property exists in HTML elements, you can't assign a handler to the property until the elements have been loaded, by which time this event will already have fired.

What did you expect to see?

This paragraph rephrased, to something similar to:

You should generally add the handler for this event to a top level object (i.e. listen for the {{domxref("Document.securitypolicyviolation_event", "securitypolicyviolation")}} event on {{domxref("Document")}} instead).
While the event may originate from HTML elements, JavaScript that assigns a handler to the element usually executes after the element has been loaded, by which time this event has already fired.
You can only successfully listen to this event if the loading starts after the listener is attached, usually by dynamically modifying the fetch target via JavaScript.

And, later, the example should demonstrate a dynamically constructed element.

const img = document.createElement("img");
img.onsecuritypolicyviolation = console.log;
img.src = "https://..."; // Fetching starts here, which is after the handler is attached

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@github-actions github-actions bot added Content:WebAPI Web API docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Aug 12, 2024
@Josh-Cena Josh-Cena removed the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant