document.currentScript returns null in microtasks
Categories
(Core :: DOM: Core & HTML, defect, P3)
Tracking
()
Tracking | Status | |
---|---|---|
firefox90 | --- | fixed |
People
(Reporter: justice360, Assigned: saschanaz)
Details
(Keywords: dev-doc-needed)
Attachments
(1 file)
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
Steps to reproduce:
Run the following JavaScript in the root scope of a script element:
console.log(document.currentScript)
Promise.resolve().then(() => {
console.log(document.currentScript)
})
Actual results:
Outputs:
<script ...>
null
Expected results:
In Chrome/Safari/...
<script ...>
<script ...>
Per HTML spec:
(document.currentScript) Returns null if the Document is not currently executing a script or SVG script element (e.g., because the running script is an event handler, or a timeout), or if the currently executing script or SVG script element represents a module script.
I'd say it's a little ambiguous about whether for micro tasks it should return null. Plus this section is labelled as "non-normative". But it's indeed unexpected that browsers handle this differently.
Sorry for the lousy formatting, I didn't expect Markdown works here. Here's the formatted version:
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
Steps to reproduce:
Run the following JavaScript in the root scope of a script element:
console.log(document.currentScript)
Promise.resolve().then(() => {
console.log(document.currentScript)
})
Actual results:
Outputs:
<script ...>
null
Expected results:
In Chrome/Safari/... it outputs:
<script ...>
<script ...>
Per HTML spec:
(document.currentScript) Returns null if the Document is not currently executing a script or SVG script element (e.g., because the running script is an event handler, or a timeout), or if the currently executing script or SVG script element represents a module script.
I'd say it's a little ambiguous about whether for micro tasks it should return null. Plus this section is labelled as "non-normative". But it's indeed unexpected that browsers handle this differently.
Hi,
Thanks for your contribution!
I don't have the technical knowledge to confirm this issue but I'll add product and component so the team can make some research on it. Hopefully, someone with a more deep understanding can help.
Regards,
Jerónimo.
Comment 3•6 years ago
|
||
Oh, interesting situation. Thanks for filing this.
I think our behavior makes the most sense here, but I will bounce it over to the DOM team just in case anyone there knows why this is left non-normative in the HTML standard.
Updated•6 years ago
|
Comment 4•6 years ago
|
||
That's not where the normative definition is.
- We start at https://html.spec.whatwg.org/#execute-the-script-block.
- Step 5 takes care of executing a classic script (those without
type=module
) and settingcurrentScript
accordingly. - Between setting
currentScript
and unsetting it, it invokes https://html.spec.whatwg.org/#run-a-classic-script. - Close to the end, that invokes https://html.spec.whatwg.org/#clean-up-after-running-script.
- As its third and final step, that runs microtasks if the JS stack is empty, which it is per OP's example.
Ergo, Firefox has a bug.
Assignee | ||
Updated•4 years ago
|
Assignee | ||
Comment 8•4 years ago
|
||
Updated•4 years ago
|
Comment 11•4 years ago
|
||
bugherder |
Description
•