3

I have content that I load dynamicaly with jquery. The content is HTML with a script tag in it.

So I have something like this.

var content = '<script language="javascript" type="text/javascript">alert("test");</script><div>mydiv</div>';

I want to create a new div with this content with jquery

var dialog = $('<div class="modal-popup">' + content + '</div>');

But in the new created div (dialog) there is no script tag.

In the past it worked. We updated jquery from 1.5.1 to 1.7.1. I already dowdated jquery, with the same result.

We use ist to create a print dialog and we recognized, that we can not print anymore. I am browsing through the repository, but can not find a change we have made.

What is the reason, the the script-tag is gone?

EDIT: It depends on the JQuery release. In v1.5.1 it works, in v1.7.2 not.

The reason, that it does not work in my prior test with v1.5.1 was, that the browser cached jquery for some reasons

EDIT 2: In this example content is a string variable to keep it simple. In real environment content is filled by $.get and is an HTML-Document with javascript in it.

Chris
  • 1,610
  • 3
  • 18
  • 37
  • With no further investigation, this sounds like a regression, or related to http://bugs.jquery.com/ticket/6516 – Phrogz Mar 29 '12 at 19:09

1 Answers1

0

The </ sequence of the closing </script> tag will end the parsing of the script element in HTML.

You must never have the characters </ anywhere inside your script markup, even if it is part of a string. Browsers honoring HTML4 should stop the script block if this is seen -Phrogz

Escaping the slash <\/ should solve the problem.

shaunsantacruz
  • 8,903
  • 3
  • 19
  • 19
  • 1
    Specifically you must never have the characters `` anywhere inside your script markup, even if it is part of a string. Browsers honoring HTML4 should stop the script block if this is seen. – Phrogz Mar 29 '12 at 18:55
  • Thanks for the post. But the example above was just to keep it simple. In real environment content ist filled bei $.get and is the response from an HTML-Document with javascript in it. – Chris Mar 30 '12 at 06:51