0

Possible Duplicate:
jQuery: Can’t append <script> element

i am trying to replace a div with a javascript by doing this:

$("#frame").replaceWith("<script language='JavaScript' type='text/javascript'>AC_VHost_Embed(2672266,206,206,'',1,1, 2279587, 0,1,0,'912832a3e8b02d79380c8352f2d497f6',9);</script>") ;

But in console, error says Unexpected token: ILLEGAL

Any idea?

Community
  • 1
  • 1
  • This might help you: [jquery-cant-append-script-element](http://stackoverflow.com/questions/610995/jquery-cant-append-script-element) – zeMinimalist Dec 01 '12 at 04:49

2 Answers2

2

I'm guessing this is inlined JavaScript, in which case you need to break up your closing </script> tag. I also made it more readable:

$("#frame").replaceWith(
    "<script language='JavaScript' type='text/javascript'>"
    + "AC_VHost_Embed(2672266,206,206,'',1,1, 2279587, "
    + "0,1,0,'912832a3e8b02d79380c8352f2d497f6',9);"
    + "<" + "/script>"
);

The reason for this is so that the browser doesn't think that your </script> inside the string is the end of the script block. Example:

<script>
    // Doesn't work!
    alert('</script>');
</script>

Your browser sees:

<script>
    // Doesn't work!
    alert('
</script>
');
</script>

In other words, your browser thinks the script ends the first time it sees </script>. You can fix this by breaking the fake </script> up into pieces.

<script>
    // Works fine!
    alert('<' + '/script>');
</script>
Nathan Wall
  • 10,530
  • 4
  • 24
  • 47
0

Appending a script tag to the DOM after it is rendered will not automatically load the script.

Have a look at this jQuery function to load scripts: http://api.jquery.com/jQuery.getScript/

kimpettersen
  • 1,602
  • 2
  • 13
  • 18