JavaScript在document.write中嵌入<script>标签不能正常工作</script>

时间:2013-10-24 22:59:47

标签: javascript

我不确定我的代码有什么问题,但是当我尝试添加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>

3 个答案:

答案 0 :(得分:8)

只需将其他脚本标记内的结束脚本标记更改为欺骗浏览器。

改变:

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

到:

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

修改

完整代码:

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

答案 1 :(得分:3)

如果您在HTML页面中嵌入javascript,HTML解析器在找到您的第一个脚本标记时会立即尝试查找结束标记。由于你的结束脚本标签在你的document.write中,你会发现自己处于泡菜状态。

您可以使用反斜杠轻松地转义标记上的结束正斜杠:

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

要:

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

答案 2 :(得分:1)

  

但是当我尝试添加actorWin.document.write('&lt; / script&gt;')时,一切都搞砸了

不确定问题是什么,但它可能对你有帮助

  

写入已经加载但未调用的文档   document.open()将自动执行document.open调用。

关于document.open call

  

如果目标中存在文档,则此方法会清除它。

Read more on MDN.

嗯,无论你遇到什么问题,你都可以使用这种方法

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

这应该有效。这里有Example