2015-12-07 31 views
2

我想知道是否有可能有一個新的window.open(這將是子窗口)嵌套在父窗口的myWin.window.open的document.write?document.write在一個新的open.document

myWin.document.write("<a href='#' onclick="window.open('http://www.imdb.com/title/tt2015381/','newMovie','height=200,width=400,left=400,top=100,scrollbars,status,resizable,dependent');">Click Here to Access the Movie Window</a> <br><br>"); 

這裏是完整的代碼,如果這有助於更好:

<script type="text/javascript">  
var myWin = window.open("", "", "height=500,width=680,resizable=yes,top=100,left=400"); 
    myWin.document.write('<html><head><title>Guardian Of the Galaxy<\/title>'); 
    myWin.document.write('<style type="text\/css">'); 
    myWin.document.write('<!-- '); 
    myWin.document.write('body#lab8img {background-image: url("Labs_Images/GofG.jpg");background-repeat: no-repeat;background-size: cover;} '); 
    myWin.document.write('p {color:#fff;font-size:20px;} '); 
    myWin.document.write('h1 {color:#fff;font-size:24px;} '); 
    myWin.document.write('a {color:#000;font-weight:bold; line-height:0.5; width:100%; text-decoration:none;} '); 
    myWin.document.write('a hover {color:#2B65EC;font-weight:bold; line-height:0.5; padding:15px;} '); 
    myWin.document.write('div#box {background: rgba(255,255,255,.5); border: 2px solid black; margin: 30px;height: 27%; width:90%;} '); 
    myWin.document.write('--><\/style>'); 
    myWin.document.write('<\/head><body id="lab8img" onload="setImageSize();">'); 
    myWin.document.write('<\/body>'); 
    myWin.document.write('<\/html>'); 
    myWin.document.close(); 
    myWin.document.write("<div style='text-align:center; '><h1>What about the Movie?</h1></div> <br>"); 
    myWin.document.write("<div style='text-align:left; padding:0px 8px 0px 8px;'><p>Guardians of the Galaxy is a 2014 American superhero film based on the Marvel Comics superhero team of the same name, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the tenth installment in the Marvel Cinematic Universe. </p></div><br>"); 
    myWin.document.write("<div Id='box' style='text-align:center;"); 


    myWin.document.write("<a href='#' onclick="window.open('http://www.imdb.com/title/tt2015381/','newMovie','height=200,width=400,left=400,top=100,scrollbars,status,resizable,dependent');">Click Here to Access the Movie Window</a> <br><br>"); 
    myWin.document.write("</div>"); 
    </script> 
+0

可能重複[如何使用Javascript將內容寫入另一個瀏覽器窗口?](http://stackoverflow.com/questions/169833/how-do-i-write-content-to-another-browser-window -using-javascript) –

+1

@Sow問題'js'不會返回預期結果嗎? – guest271314

+0

@EricPhillips我看着另一個答案,它是不同的。我想通過document.write將子窗口打開爲父窗口的鏈接。之後,你點擊它,並打開子窗口。 – Sow

回答

1

你的點子工作正常。使用javascript生成帶有鏈接的彈出窗口以生成另一個彈出窗口沒有任何內在問題。

問題是一個小的語法錯誤:你是不是在哪裏你正在寫的a標籤行逃脫你的雙引號,因此它打破了,因爲你的字符串是不是你希望它是什麼

myWin.document.write("<a href='#' onclick="window.open('... 

你是用雙引號("<a href...)打開您的字符串但你在onclick事件(onclick="window.open(...)再次使用雙引號所以只是逃避onclick事件的雙引號(注意反斜槓)

myWin.document.write("<a href='#' onclick=\"window.open('http://www.imdb.com/title/tt2015381/','newMovie','height=200,width=400,left=400,top=100,scrollbars,status,resizable,dependent');\">Click Here to Access the Movie Window</a> <br><br>"); 

當然還要注意,如果彈出窗口被阻止,代碼也會中斷,因爲如果彈出窗口被阻止,myWin將不會被分配新窗口。

+0

謝謝!我不知道你需要添加\ ..? – Sow