为了账号安全,请及时绑定邮箱和手机立即绑定

我的代码里的 关闭新打开窗口 怎么实现?

<!DOCTYPE html>
<html>
 <head>
  <title> new document </title>  
  <meta http-equiv="Content-Type" content="text/html; charset=gbk"/>   
  <script type="text/javascript">  
    function openWindow(){
        // 新窗口打开时弹出确认框,是否打开
        var newWin = confirm("你要打开新的网页窗口吗?");
        if(newWin == true){
            op();
        }else{
            alert("好的再见!");
        };    
    }
    
    function op(){
      // 通过输入对话框,确定打开的网址,默认为 http://www.imooc.com/
            var purl = "http://www.imooc.com";
            var input = prompt("请输入网址打开新窗口",purl);
            var x=0;
            var y=0;
            if(input != null){//打开的窗口要求,宽400像素,高500像素,无菜单栏、无工具栏
                    var win = open(purl,"_blank","top=y,left=x,width=400,height=500,menubar=no,toolbar=no");
            };
    }
    
    function closeWin(){
        win.close();
    }
  </script>
 </head>
 <body>
 <form>
      <input type="button" value="请单击我打开新的窗口!" onclick="openWindow()" />
    <input type="button" value="关闭新打开的窗口" onclick="closeWin()" />
</form>
 </body>
</html>

正在回答

6 回答

语法是 <窗口对象>.close() ,因此,你要在function外先声明窗口变量。以下是我的代码,看关键的两行即可。

<!DOCTYPE html>
<html>

	<head>
		<title> new document </title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<script type="text/javascript">
			var win; //声明新窗口变量。
			function openWindow() {
				var isnot = confirm("确定要打开慕课网吗?"); // 新窗口打开时弹出确认框,是否打开
				if (isnot) {
					//打开窗口时,相当于为变量赋值。
					win = window.open('http://www.imooc.com/', '_black', 'width=400,height=500,toolbar=no,scrollbars=no');
					// 通过输入对话框,确定打开的网址,默认为 http://www.imooc.com/
					//打开的窗口要求,宽400像素,高500像素,无菜单栏、无工具栏。
				} else {
					alert("您放弃打开慕课网!");
				}
			}

			function closeWindow() {
				win.close(); //关闭新窗口
			}
		</script>
	</head>

	<body>
		<input type="button" value="新窗口打开网站" onclick="openWindow()" />
		<input type="button" value="关闭新窗口网站" onclick="closeWindow()" />
	</body>

</html>


0 回复 有任何疑惑可以回复我~
#1

MichaelYan0 提问者

非常感谢!
2015-09-29 回复 有任何疑惑可以回复我~
function closeWin(){
        window.close();
    }


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

我的代码里的 关闭新打开窗口 怎么实现?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信