Closed Bug 655519 Opened 14 years ago Closed 10 years ago

Dynamically added script tags not executed.

Categories

(Core :: DOM: Core & HTML, defect)

defect
Not set
normal

Tracking

()

RESOLVED WORKSFORME

People

(Reporter: mail, Unassigned)

Details

(Keywords: testcase-wanted)

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 Build Identifier: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 When a script tag is injected into the DOM via javascript, the script that is loaded is not executed. On occasion, when viewing the tag contents in Firebug, I see the message "Failed to load source...". Though, many times the script content is loaded. This was working fine prior to updating to Firefox 4.0. Reproducible: Always Steps to Reproduce: 1.Use javascript to inject a script tag into the head with a src attribute. 2.Notice that script contents are not executed. 3.Can remove and re-add the src attribute URL and it will then execute (with Firebug). Actual Results: See details. Expected Results: Script contents will note be executed.
Please provide a test case.
Component: General → DOM
Product: Firefox → Core
QA Contact: general → general
Severity was too high, downgrading: Critical (crashes, loss of data, severe memory leak) -> Normal (regular issue, some loss of functionality under specific circumstances).
Severity: critical → normal
Jay, can you reproduce the issue using a Nightly? or Aurora? Nightly is available here: https://nightly.mozilla.org/ Aurora is available here: https://www.mozilla.com/firefox/channel/
Keywords: testcase-wanted
OS: Mac OS X → All
Hardware: x86 → All
Hi Jay, Can you please provide a reduced testcase as asked in comment 1, so we can try to reproduce this issue? Also, can you please test this on the latest Firefox release (44.0.2) or latest Nightly (47.0a1, https://nightly.mozilla.org/) and tell me if this still reproduces for you ? When doing this please use a new fresh Firefox profile, maybe also in safe mode (https://support.mozilla.org/en-US/kb/troubleshoot-and-diagnose-firefox-problems). Thanks, Cosmin.
Flags: needinfo?(mail)
We don't even need a reduced testcase, for a start. Just _a_ testcase.
Since it has been some time since I opened this ticket, I'm a little fuzzy on the original details. At some point, I switched to using requireJS to manage loading JS resources. I've not had any issues with dynamically loading and executing scripts in FF on that front. I no longer have a reference to locate the original issue in my VCS. I would have to either dig through to find it or try to create a new test case from scratch. Though, it is no longer a relevant issue to me, if the issue still exists at all. I'm fine with closing the ticket and/or marking it as invalid. If there's some reason to believe that the issue could still be of concern, please let me know. I will try to carve out some time to come up with a test case. My apologies for missing the original needinfo requests. Thank You, Jay Kelly
Flags: needinfo?(mail)
Jay, thanks for following up. Since no one else has encountered this, marking worksforme for now. If it comes up again, please reopen.
Status: UNCONFIRMED → RESOLVED
Closed: 10 years ago
Resolution: --- → WORKSFORME
Component: DOM → DOM: Core & HTML

The dynamically added script in the example below will not be executed on FireFox (nor on Safari), but will on Chrome:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Test dynamically added script</title>
  <script>
    setTimeout(_=>{
      alert("Adding a new script");
      let newScript = document.createElement('script');
      newScript.innerHTML = "window.onload = _=>{alert('Script added and executed');}";
      document.getElementsByTagName('head')[0].appendChild(newScript);
    }, 0);
  </script>
</head>
<body>
</body>
</html>

The message "Adding a new script" appears but "Script added and executed" does not. To make it work, one needs to remove timeout.
Don't ask why I needed this :)

Correction to the above: Probably the execution of the added script on Chrome is still an artifact because it happens when the above example is served from a server not from a local file, and setting the timeout to >~ 10 ms stops the execution.

Right, that code is fundamentally racy: it depends on whether the setTimeout fires before the load event fires, which depends on whether the rest of the HTML was received fast enough, on timer scheduling, etc, etc.

That is, the dynamically added script is executing in all cases. What changes is whether it sets window.onload before the load event has fired or after. If it sets it after, the alert won't be shown.

You need to log in before you can comment on or make changes to this bug.