简体   繁体   English

'javascript:close()'似乎不起作用

[英]'javascript:close()' doesn't seem to be working

I'm really new to JavaScript, but I think this should be pretty simple. 我真的是JavaScript新手,但我认为这应该很简单。 I'm just trying to create a link to close my window using: document.write("Close this window"); 我只是想使用以下方法创建一个链接以关闭我的窗口:document.write(“ Close this window”);

However, when I click the link created by the above code nothing happens. 但是,当我单击以上代码创建的链接时,没有任何反应。 What am I doing wrong? 我究竟做错了什么? my entire code is below: 我的整个代码如下:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Loops demo</title>

</head>
<body>
<script type="text/javascript">
function openActor() {
            actorWin = window.open("","actorWin","height=200,width=600,resizable");
            actorWin.document.write("<!doctype html>");
            actorWin.document.write("<html lang=\"en\">");
            actorWin.document.write("<head>");
            actorWin.document.write("<meta charset=\"utf-8\">");
            actorWin.document.write("<title>Keira Knightley</title>");;
            actorWin.document.write("</head>");
            actorWin.document.write("<body>");        
            actorWin.document.write("Info on, one of the great actors of our time.");        
            actorWin.document.write("</body>");
            actorWin.document.write("</html>");
      }


   function Movie(title, actor, web1, web2) {
    this.title = title;
    this.actor = actor;
    this.link = web1;
    this.link2 = web2;
}
var movie1 = new Movie('Pride and Prejudice', 'Keira Knightley', 'http://movies.yahoo.com/movie/pride-and-        prejudice-2005/','http://keiraknightleyfan.com/');


document.write(movie1.title + '<blockquote>“A lady\'s imagination is very rapid; it jumps from admiration to love,     from love to matrimony in a moment.” ― Jane Austen, Pride and Prejudice</blockquote>');
document.write('<a href="javascript:var movieWin = window.open(\'',movie1.link, ' \'     ,\'movieWin\',\'height=500,width=500,resizable,top=200,left=400\')">Read more about Pride Prejudice</a><br>');
document.write("<a href='javascript:openActor()'>Click here for info on the lead actor</a><br>");
document.write("<a href='javascript:close()'>Close this window</a>");

</script>   
</body>
</html>

如果尚未通过JavaScript打开窗口,则无法使用close()关闭窗口

尝试window.close()

document.write("<a href='javascript:window.close()'>Close this window</a>");

There is no function named close . 没有名为close函数。 javascript:close(); simply calls the function close . 只需将函数close调用即可。 If you want to close the popup window, you will need to call close on that. 如果要关闭弹出窗口,则需要在其上调用close。 Like: javascript:actorWin.close(); 像: javascript:actorWin.close();

In that case, make sure you declare the actorWin in the global scope (put var actorWin; just above function openActor... ). 在这种情况下,请确保在全局范围内声明actorWin (将var actorWin;放到function openActor...正上方)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM