Closed
Bug 489153
Opened 16 years ago
Closed 15 years ago
appendChild(<script src=...>) executes the appended script asynchronously (Newly added JavaScript function cannot be called in same thread)
Categories
(Core :: General, defect)
Tracking
()
RESOLVED
INVALID
People
(Reporter: anschoen, Unassigned)
Details
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8
The following HTML page adds a new external JavaScript file to the page when one of boxes is clicked:
<html>
<head>
<script type="text/javascript">
function addExternalScript(url) {
var scriptTag = document.createElement("script");
scriptTag.setAttribute("type","text/javascript");
scriptTag.setAttribute("src",url);
document.getElementsByTagName("head")[0].appendChild(scriptTag);
}
</script>
</head>
<body>
<div style="background-color:blue;width:100px;height:100px" onclick="addExternalScript('script.js');f1();">Click to call external function in the same thread that added the script</div>
<div style="background-color:yellow;width:100px;height:100px" onclick="addExternalScript('script2.js');setTimeout('f2()',1);">Click to call external function in a different thread</div>
</body>
</html>
In the first case, the a function in the newly included file is invoked right after the JavaScript file was added. In the second case, a "new thread" was used to call the external function. Only the second case works.
Used scripts:
script1.js:
function f1() {
alert("hello");
}
script2.js:
function f2() {
alert("hello");
}
Reproducible: Always
Steps to Reproduce:
1.Click on first box in page
2.
3.
Actual Results:
Nothing happens
Expected Results:
An alert box should be shown.
Updated•16 years ago
|
Product: Firefox → Core
QA Contact: general → general
Reporter | ||
Updated•15 years ago
|
Severity: minor → major
Comment 1•15 years ago
|
||
Sorry you were ignored. These things happen, unfortunately, and you should feel free to post to a relevant mailing list or ask in IRC to bring attention to your problem.
As for the problem itself. I can reproduce it, but it's by design. Note that every other browser does this (I checked Chrome and Opera on Mac) and it's described in the HTML5 spec: http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#running-a-script
[...]
8. Then, the first of the following options that describes the situation must be followed:
[...]
-> If the element has a src attribute
The element must be added to the end of the list of scripts that will execute as soon as possible.
[... note that <script>inline script</script> is executed synchronously]
-> Otherwise
The user agent must immediately execute the script block, even if other scripts are already executing.
You might be also interested in tests (showing the behavior we expect) -- added in bug 371576. E.g. http://mxr.mozilla.org/mozilla-central/source/content/base/test/test_bug371576-1.html?force=1 tests, in particular, your case ("B" directly following "A")
Status: UNCONFIRMED → RESOLVED
Closed: 15 years ago
Resolution: --- → INVALID
Summary: Newly added JavaScript function cannot be called in same thread → appendChild(<script src=...>) executes the appended script asynchronously (Newly added JavaScript function cannot be called in same thread)
You need to log in
before you can comment on or make changes to this bug.
Description
•