(利用DOM)在新打开的页面点击关闭当前浏览器窗口

1、在开发过程中我们前端的用户体验中有时候会要求点击一个按钮,关闭当前浏览器窗口,用HTML DOM就可做到

2、注意:本次写法要求在新窗口中关闭。target="_blank"

3、由a.html中打开b.html在b页面中点击按钮关闭B页面

4、a页面代码

复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 </head>
 <body>
  <div>
  <a href='button_colse.html' target="_blank">测试点击按钮关闭当前页面</a>
  </div>
 </body>
</html>
复制代码

5. b页面代码

复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <style type="text/css"></style>
  <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
 </head>
 <body>
  <div>
  点击我关闭本页面
  </div>
  <button  onclick="javascript:window.opener=null;window.open('','_self');window.close();" type="button" role="button" aria-disabled="false">
        <span class="ui-button-text">
        <span>关闭</span>
        </span>
 </button>
 </body>
</html>
复制代码
原文地址:https://www.cnblogs.com/zzp0320/p/7728361.html