简体   繁体   中英

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

I'm really new to JavaScript, but I think this should be pretty simple. I'm just trying to create a link to close my window using: 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 . javascript:close(); simply calls the function close . If you want to close the popup window, you will need to call close on that. Like: javascript:actorWin.close();

In that case, make sure you declare the actorWin in the global scope (put var actorWin; just above function openActor... ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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