2012-10-26 138 views
0

我真的很陌生,但我认为这应该很简单。我只是想创建一个链接来关闭我的窗口使用:document.write(“Close this window”);'javascript:close()'似乎没有工作

但是,当我点击由上述代码创建的链接时,什么都不会发生。我究竟做错了什么?我的整个代码如下:

<!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> 
+1

我建议不使用document.write,因为这将覆盖任何东西在页面别的。 –

+1

@Lee Taylor:当文档已经关闭时,它只会覆盖文档的内容,在这种情况下它仍然是开放的,所以当他使用写入时没有问题。 –

回答

0

没有名为close功能。 ​​只需调用函数close。 如果您想关闭弹出窗口,您需要在该窗口上关闭。 像:javascript:actorWin.close();

在这种情况下,一定要在全球范围内(放var actorWin;略高于function openActor...)宣布actorWin

0

尝试window.close()

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

你不能当他们没有被打开使用close()关闭窗口通过JavaScript