JavaScript 在 document.write 中嵌入 <script> 标签不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19578244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:01:38  来源:igfitidea点击:

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

javascript

提问by noWayhome

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. Without this line, the code works fine.

我不确定我的代码有什么问题,但是当我尝试添加actorWin.document.write('<script type=\"text/javascript\"><\/script>')所有内容时,一切都被搞砸了。没有这一行,代码工作正常。

<!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>

回答by KiT O

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>");

回答by Trevor

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. Since your closing script tag is in your document.write you'll find yourself in a pickle.

如果您在 HTML 页面中嵌入了 javascript,HTML 解析器在找到您的第一个脚本标签时会立即尝试找到结束标签。由于您的结束脚本标签在您的 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>')

回答by The Alpha

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 Examplehere.

这应该有效。这里有一个例子