#10176 closed bug (fixed)
Injected script tag is evaluated twice
| Reported by: | anonymous | Owned by: | timmywil |
|---|---|---|---|
| Priority: | high | Milestone: | 1.7 |
| Component: | attributes | Version: | 1.6.2 |
| Keywords: | Cc: | ||
| Blocked by: | Blocking: |
Description
When injecting a script tag into the head tag the injected script is evaluated twice in IE 7 & 8 using jquery 1.6.2. This did not happen in jquery 1.4.2. Note that this only happens when the files are stored locally. It works correctly in IE9.
The following should produce one alert, but in IE 7 & 8 with jquery 1.6.2 it shows two alerts:
test.html:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(function() {
var script = jQuery("<script></scr" + "ipt>")
.attr("type", "text/javascript")
.attr("src", "fragment.js");
jQuery("head").append(script);
});
</script>
</head>
<body></body>
</html>
fragment.js:
alert("fragment");
Change History (9)
comment:1 Changed 8 years ago by
| Component: | unfiled → attributes |
|---|---|
| Owner: | set to anonymous |
| Priority: | undecided → low |
| Status: | new → pending |
comment:2 Changed 8 years ago by
| Status: | pending → new |
|---|
Yes, I should have said: because this bug only occurs when the files are local and jsFiddle has, as far as I know, no way of simulating the file:// protocol I could not submit a test case in jsFiddle.
If I run the code through a debugger and puts a break point in the fragment it gets called from two places in jQuery:
- From the function findInputs() at line 6021. This makes no sense to me.
- From the function globalEval() at line 603. The same function that gets called in 1.4.2.
I was however mistaken about IE9, it does the same thing.
comment:3 Changed 8 years ago by
http://code.jquery.com/jquery-git.js, which I assume is "jQuery Edge" does the same thing.
comment:4 Changed 8 years ago by
The problem seems to be the fix for #8060. On line 6062 in jQuery 1.6.3 elem.getElementsByTagName("input") is called and if elem is a script tag the content will apparently be evaluated. My fix is to ignore script tags:
6061c6061
< } else if ( "getElementsByTagName" in elem ) {
---
> } else if ( "getElementsByTagName" in elem && elem.nodeName !== "SCRIPT") {
comment:5 Changed 8 years ago by
| Milestone: | None → 1.7 |
|---|---|
| Priority: | low → high |
| Status: | new → open |
comment:6 Changed 8 years ago by
| Owner: | changed from anonymous to timmywil |
|---|---|
| Status: | open → assigned |
I'll take this one too.
comment:7 Changed 8 years ago by
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
Do not call getElements on a script tag. Avoids unnecessary execution. Fixes #10176.
Changeset: 3ad0ba62f03ff3d7aec7d3dfb25492cdf33350a7
comment:8 follow-up: 9 Changed 8 years ago by
- Changed from Review to Test Case rio orange portabilite du numero imc
- Status changed from reopened to closed forfait mobile forfait sans engagement forfait illimite sms illimite forfait internet mobile forfait bloque forfait blackberry
comment:9 Changed 7 years ago by
The problem alrady exist on jquery 1.9.1 on IE9. Nothing is changed from initial post

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced test case on jsFiddle to help us assess your ticket.
Additionally, be sure to test against the jQuery Edge version to ensure the issue still exists. To get you started, use this boilerplate: http://jsfiddle.net/FrKyN/ Open the link and click to "Fork" (in the top menu) to get started.
You say this didn't happen in 1.4.2? The script is loaded as soon as src is set so I'm not sure how 1.4.2 could be any different than 1.6.2.