0

IE8 throws error on 4th line.

jQuery('#list script').each(function() {

    var script=document.createElement('script');
    script.type='text/javascript';
    jQuery(script).text(jQuery(this).text()); //Error in IE8  does the field editing
    document.body.appendChild(script);
}).remove();

jquery error in routine:

append: function() {
    return this.domManip(arguments, true, function( elem ) {
        if ( this.nodeType === 1 ) {
            this.appendChild( elem );
        }
    });
jfriend00
  • 683,504
  • 96
  • 985
  • 979
Rob
  • 1
  • 1
  • 1

4 Answers4

3

You don't have to recreate the script elements or do all of that explicit removing. You can simply do the following:

jQuery('#list script').appendTo('body');
FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
  • +1 slick... but i think it has to be `appendTo(jQuery('body'))` instead of `appendTo('body')` – gion_13 Aug 09 '11 at 21:27
  • I double-checked, and the argument to `appendTo` can be a "selector, element, HTML string, or jQuery object". http://api.jquery.com/appendTo/ – FishBasketGordo Aug 09 '11 at 21:29
1

how about using jquery a bit more...

jQuery('#list script').each(function() {
    jQuery('<script type="text/javascript" />').text(jQuery(this).text()).appendTo(jQuery('body'));
}).remove();
Dean North
  • 3,741
  • 2
  • 29
  • 30
  • actually `$(document.createElement('div'))` is considerably faster than `$('
    ')` I'll see if I can find the SO question to back this up... EDIT: Not the one I remember but here's the same answer: http://stackoverflow.com/questions/268490/jquery-document-createelement-equivalent
    – anthony sottile Aug 09 '11 at 21:27
  • 1
    The speed improvement is negligible, not considerable. I prefer the shorter code. Unless I'm dealing with thousands of elements, it doesn't make much difference at all. – Dean North Aug 09 '11 at 21:49
1

why don't you use jquery all the way?

jQuery('#list script').each(function() {
    jQuery('<script></script>')
        .attr('type','text/javascript')
        .text(jQuery(this).text())
        .appendTo(jQuery('body'));
}).remove();
gion_13
  • 41,171
  • 10
  • 96
  • 108
0

Guessing it is a security exception since you didn't list anything.

Though without the question explaining the exact line (in the jquery routine) and exact exception this is a mere stab in the dark.

It could be that you are accessing a script that is outside the page's domain and therefore will not allow you to access its text.

It could also be the way you create the script tag.

I'd suggest something a little more concise:

$('#list script').each(function() {
    $('<script>').text($(this).text()).appendTo($(document.body));
})
.remove();

Note I left out the type on assuming you are using html5, if not you'll optionally want to put it in there

anthony sottile
  • 61,815
  • 15
  • 148
  • 207