Bug Tracker

Ticket #10176 (closed bug: fixed)

Opened 3 years ago

Last modified 22 months ago

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:
Blocking: Blocked by:

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

comment:1 Changed 3 years ago by timmywil

  • Owner set to anonymous
  • Priority changed from undecided to low
  • Status changed from new to pending
  • Component changed from unfiled to attributes

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.

comment:2 Changed 3 years ago by anonymous

  • Status changed from pending to 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 3 years ago by anonymous

 http://code.jquery.com/jquery-git.js, which I assume is "jQuery Edge" does the same thing.

comment:4 Changed 3 years ago by anonymous

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 3 years ago by dmethvin

  • Priority changed from low to high
  • Status changed from new to open
  • Milestone changed from None to 1.7

comment:6 Changed 3 years ago by timmywil

  • Owner changed from anonymous to timmywil
  • Status changed from open to assigned

I'll take this one too.

comment:7 Changed 3 years ago by timmywil

  • Status changed from assigned to closed
  • Resolution set to fixed

Do not call getElements on a script tag. Avoids unnecessary execution. Fixes #10176.

Changeset: 3ad0ba62f03ff3d7aec7d3dfb25492cdf33350a7

comment:8 follow-up: ↓ 9 Changed 3 years ago by anonymous

comment:9 in reply to: ↑ 8 Changed 22 months ago by anonymous

The problem alrady exist on jquery 1.9.1 on IE9. Nothing is changed from initial post

Note: See TracTickets for help on using tickets.