JavaScript中window.open()打开与window.close()关闭

close()关闭窗口,语法书写如下,其次使用close()在打开新窗口的同时,关闭该窗口,是看不到被打开窗口的

1 window.close();//关闭本窗口
2 <窗口对象>.close();//关闭指定的窗口

 代码展示:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>JavaScript中window.open()与window.close()</title>
 6     <script type="text/javascript">
 7         function myopen(){
 8             window.open('https://www.baidu.com/','_blank','width=300,height=200,left=0,meunbar=no,toolbar=no,scrollbar=yes,status=no');
 9         }
10         
11         var ceshi=window.open('https://www.cnblogs.com/dhnblog/p/12494648.html')//将新打的窗口对象,存储在变量ceshi中
12         // // ceshi.wondows.close()   错误写法
13         ceshi.close()
14     </script>
15 </head>
16 <body>
17     <input type="button" name="" id="" value="点击打开新窗口" onclick="myopen()" />
18 </body>
19 </html>

 使用<窗口对象>.close();//关闭指定的窗口 代码展示:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>通过变量关闭窗口</title>
 6     <script type="text/javascript">
 7         function myopen(){
 8             var ceshi=window.open('https://www.baidu.com/','_blank','width=300,height=200,left=0,meunbar=no,toolbar=no,scrollbar=yes,status=no');
 9             ceshi.close()
10         }
11     </script>
12 </head>
13 <body>
14     <input type="button" name="" id="" value="我不信你可以打开" onclick="myopen()" />
15 </body>
16 </html>

 至于window.close();//关闭本窗口 暂时不是很懂,感兴趣的可以参考下这个,后期有机会在完善

==修改时间2020/04/08/20:58 window.close();//关闭本窗口

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8">
 5         <title>JavaScript-关闭窗口(window.close)</title>
 6         <script type="text/javascript">
 7             // window.close();   //关闭本窗口
 8             // <窗口对象>.close();   //关闭指定的窗口
 9             
10             // window.open('http://www.dhnblog.com/','_blank','top=100,left=100,width=200,height=300,menubar=yes,toolbar=yes,status=no,scrollbars=yes');
11             //window.close();
12                 
13             var mywin=window.open('http://www.dhnblog.com/','_blank','top=100,left=100,width=200,height=300,menubar=yes,toolbar=yes,status=no,scrollbars=yes');
14             // //将新打的窗口对象,存储在变量mywin中
15             mywin.close();
16         </script>
17     </head>
18     <body>
19         <p>上面代码在打开新窗口的同时,关闭该窗口,看不到被打开的窗口。</p>
20     </body>
21 </html>
183人参与, 0条评论 登录后显示评论回复

你需要登录后才能评论 登录/ 注册