document.write within a new open.document



我想知道是否有可能在myWin.window.open的父窗口的文档中嵌套一个新窗口.open(它将是子窗口)?

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>

你的想法很好。使用javascript生成带有生成另一个弹出窗口链接的弹出窗口并没有本质上的问题。

问题是一个小的语法错误:在写a标记的行上,你没有转义双引号,所以它会断开,因为你的字符串不是你期望的

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

您正在用双引号("<a href...)打开字符串,但随后在onclick事件(onclick="window.open(...)中再次使用双引号,因此只需在onclicck事件中转义双引号(注意后面的斜杠)

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将不会被分配新窗口。

最新更新