Bug Tracker

Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#6417 closed bug (duplicate)

Script tags behave oddly when added to the document

Reported by: nsalyzyn Owned by: nsalyzyn
Priority: undecided Milestone: 1.4.2
Component: manipulation Version: 1.4.2
Keywords: script tag, append Cc: [email protected]
Blocked by: Blocking:

Description

The following two jquery calls should have duplicate effects:

$('body')
.append(
  $('<div>some text</div>')
  .append('<script type="other">something</' + 'script>')
);

$('body')
.append(
  (
   $('<div>some text</div>')
   .append('<script type="other">something</' + 'script>')
  )[0]
);

However, it produces the following dom:

<body>
  <div>some text</div>
  <script type="other">something</script>
  <div>some text<script type="other">something</script></div>
<body>

The second type of addition makes the most sense. This is important since javascript programs such as MathJax use script tags with other type fields to perform various actions.

Change History (5)

comment:1 Changed 12 years ago by nsalyzyn

I found the code responsible:

in jQuery.clean near the bottom of the function there is:

if ( fragment ) {
  for ( var i = 0; ret[i]; i++ ) {
    if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
      scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
				
    } else {
      if ( ret[i].nodeType === 1 ) {
        ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
      }
      fragment.appendChild( ret[i] );
    }
  }
}

You use ret[i].getElementsByTagName("script"), but do not eliminate those that have blank or text/javascript type fields.

comment:2 Changed 11 years ago by Rick Waldron

Owner: set to nsalyzyn
Priority: undecided
Status: newpending

Please provide a reduced jsFiddle test case, thanks!

Additionally, test against the jQuery 0 GIT version to ensure the issue still exists.

comment:3 Changed 11 years ago by Rick Waldron

Keywords: needsreview added

comment:4 Changed 11 years ago by dmethvin

Keywords: needsreview removed
Resolution: duplicate
Status: pendingclosed

comment:5 Changed 11 years ago by dmethvin

Duplicate of #6941.

Note: See TracTickets for help on using tickets.