簡體   English   中英

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

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

我不確定我的代碼有什么問題,但是當我嘗試添加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>

只需更改其他腳本標記內的結束腳本標記即可欺騙瀏覽器。

改變:

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

如果您在HTML頁面中嵌入javascript,HTML解析器在找到您的第一個腳本標記時將立即嘗試查找結束標記。 由於你的結束腳本標簽在你的document.write中,你會發現自己處於泡菜狀態。

您可以使用反斜杠輕松地轉義標記上的結束正斜杠:

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

至:

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

但是當我嘗試添加actorWin.document.write('</ script>')時,一切都搞砸了

不確定是什么問題,但它可能會對你有所幫助

寫入已加載但未調用document.open()的文檔將自動執行document.open調用。

關於document.open call

如果目標中存在文檔,則此方法將清除它。

閱讀有關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);

這應該工作。 這里有一個例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM