Bug Tracker

Ticket #6417 (closed bug: duplicate)

Opened 5 years ago

Last modified 4 years ago

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

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

comment:1 Changed 5 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 4 years ago by rwaldron

  • Owner set to nsalyzyn
  • Priority set to undecided
  • Status changed from new to pending

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 4 years ago by rwaldron

  • Keywords needsreview, added

comment:4 Changed 4 years ago by dmethvin

  • Keywords needsreview, removed
  • Status changed from pending to closed
  • Resolution set to duplicate

comment:5 Changed 4 years ago by dmethvin

Duplicate of #6941.

Note: See TracTickets for help on using tickets.