Modify ↓
Ticket #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: | 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:2 Changed 3 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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
Note: See
TracTickets for help on using
tickets.

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.