Skip to main content

Bug Tracker

Side navigation

#6417 closed bug (duplicate)

Opened April 09, 2010 10:41PM UTC

Closed November 05, 2010 02:40AM UTC

Last modified November 05, 2010 02:40AM UTC

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@gmail.com
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.

Attachments (0)
Change History (5)

Changed April 12, 2010 04:23PM UTC by nsalyzyn comment:1

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.

Changed October 29, 2010 04:55PM UTC by rwaldron comment:2

owner: → 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.

Changed October 29, 2010 05:00PM UTC by rwaldron comment:3

keywords: script tag, appendneedsreview, script tag, append

Changed November 05, 2010 02:40AM UTC by dmethvin comment:4

keywords: needsreview, script tag, appendscript tag, append
resolution: → duplicate
status: pendingclosed

Changed November 05, 2010 02:40AM UTC by dmethvin comment:5

Duplicate of #6941.