简体   繁体   English

JavaScript嵌入<script> tags in document.write not working

[英]JavaScript embedding <script> tags in document.write not working

I'm not sure what is wrong with my code, but when I try and add actorWin.document.write('<script type=\\"text/javascript\\"><\\/script>') everything gets screwed up. 我不确定我的代码有什么问题,但是当我尝试添加actorWin.document.write('<script type=\\"text/javascript\\"><\\/script>')一切都搞砸了。 Without this line, the code works fine. 没有这一行,代码工作正常。

<!DOCTYPE html>
<meta charset="utf-8">
<title>create a window</title>

<script type='text/javascript'>
  function Movie(title, actor) {
    this.title = title;
    this.actor= actor;    
  }
</script>
</head>
<body>
<script type='text/javascript'>

  var documentary = new Movie('http://www.imdb.com/title/tt0358456/?ref_=fn_al_tt_2','http://en.wikipedia.org/wiki/Joaquin_Phoenix');
  var movieWin = new Object();
  var actorWin = new Object();  

  newWin=window.open('','Win','width=300,height=200,top=100,left=600');

   newWin.document.write(
   "<script type='text/javascript'>" +
        "function PopUpWindowMovie(url) {" +
             "movieWin=window.open(url,'','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
             "movieWin.focus();}" + 
        "function PopUpWindowActor(){" +
             "actorWin=window.open('','','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
             "actorWin.focus(); " +
             "actorWin.document.write('Joaquin Phoenix is a great actor and a long time vegan.<br />');" +
             "actorWin.document.write('<script type=\"text/javascript\">" +
             "function test() {" +
                    "alert(\"here\");" +
             "} <\/script>');" +
        "}" +
    "<\/script>"); 
   newWin.document.write("This is a MUST SEE movie: <h1>Earthlings (2005)</h1>");
   newWin.document.write("<a href=\"javascript:PopUpWindowMovie('"+documentary.title+"')\">Go to see the movie info</a><br />");
   newWin.document.write("<a href=\"javascript:PopUpWindowActor()\">Go to see the lead actor</a>");
</script>

</body>
</html>

Just change closing script tag inside other script tag to fool browser. 只需更改其他脚本标记内的结束脚本标记即可欺骗浏览器。

change : 改变:

actorWin.document.write('<script type=\"text/javascript\"><\/script>')

to : 至 :

actorWin.document.write('<script type=\"text/javascript\"><\/scr'+'ipt>')

Edit : 编辑

Full code : 完整代码:

 newWin.document.write(
   "<script type='text/javascript'>" +
        "function PopUpWindowMovie(url) {" +
             "movieWin=window.open(url,'','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
             "movieWin.focus();}" + 
        "function PopUpWindowActor(){" +
             "actorWin=window.open('','','height=600,width=800,left=400,top=100,scrollbars,status,resizable');" +
             "actorWin.focus(); " +
             "actorWin.document.write('Joaquin Phoenix is a great actor and a long time vegan.<br />');" +
             "actorWin.document.write('<script type=\"text/javascript\">" +
             "function test() {" +
                    "alert(\"here\");" +
             "} <\/scr'+'ipt>');" + // <-- I've edited this line
        "}" +
    "<\/script>"); 
   newWin.document.write("This is a MUST SEE movie: <h1>Earthlings (2005)</h1>");
   newWin.document.write("<a href=\"javascript:PopUpWindowMovie('"+documentary.title+"')\">Go to see the movie info</a><br />");
   newWin.document.write("<a href=\"javascript:PopUpWindowActor()\">Go to see the lead actor</a>");

If your embedding javascript in an HTML page the HTML parser when it finds your first script tag will immediately try to find the closing tag. 如果您在HTML页面中嵌入javascript,HTML解析器在找到您的第一个脚本标记时将立即尝试查找结束标记。 Since your closing script tag is in your document.write you'll find yourself in a pickle. 由于你的结束脚本标签在你的document.write中,你会发现自己处于泡菜状态。

You can easily just escape the closing forward slash on the tag with a backslash: 您可以使用反斜杠轻松地转义标记上的结束正斜杠:

document.write("<script>alert('foo')</script>') 

To: 至:

document.write("<script>alert('foo')<\/script>')

but when I try and add actorWin.document.write('</script>') everything gets screwed up 但是当我尝试添加actorWin.document.write('</ script>')时,一切都搞砸了

Not sure what's the problem but it may help you 不确定是什么问题,但它可能会对你有所帮助

Writing to a document that has already loaded without calling document.open() will automatically perform a document.open call. 写入已加载但未调用document.open()的文档将自动执行document.open调用。

About document.open call 关于document.open call

If a document exists in the target, this method clears it. 如果目标中存在文档,则此方法将清除它。

Read more on MDN. 阅读有关MDN的更多信息。

Well, whatever your problem is, you may use this approach 好吧,无论你遇到什么问题,你都可以使用这种方法

var newWin = window.open('','Win','width=300,height=200,top=100,left=600');

// Then add scripts
var script1 = document.createElement('script');
script1.innerHTML = "function someFunc(){  alert('Hello'); }";
newWin.document.getElementsByTagName('head')[0].appendChild(script1);

var script2 = document.createElement('script');
script2.src = "http://code.jquery.com/jquery-git2.js";
newWin.document.getElementsByTagName('head')[0].appendChild(script2);

This should work. 这应该工作。 An Example here. 这里有一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM