我又两个页面
a.htm
b.htm点击a里边的链接,弹出b
在a里边,怎么得到b的状态(开启/已关闭)js这种效果该怎么写

解决方案 »

  1.   

    a:
    var bWin = window.open(b.htm);if(bWin.closed){
     // b.htm窗口关闭了
    }
      

  2.   

    a.html:<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Insert title here</title>
        </head>
        <body>
         <p>This is a.html</p>
         <a href="b.html" target="_blank" class="link">link</a>
         <script>
         function setNewPageStatus(boolean){
         var link = document.getElementsByTagName('a')[0];
         if(boolean){
         var className = link.getAttribute('class');
         className = className ? className + ' clicked' : 'clicked';
         link.setAttribute('class', className);
         }else{
         if(link.className === 'clicked'){
         link.removeAttribute('className');
         }else{
         var className = link.getAttribute('class').replace(/ clicked|clicked /, '');
         link.setAttribute('class', className);
         }
         }
         }
         function getNewPageStatus(){
         var link = document.getElementsByTagName('a')[0],
         className = link.getAttribute('class');
         return className.indexOf('clicked') === -1 ? false : true;
         }
         function alertNewPageStatus(){
         alert(getNewPageStatus());
         }
         </script>
        </body>
    </html>
    b.html:<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Insert title here</title>
        </head>
        <body>
         <p>This is b.html</p>
         <script>
         window.onload = function(){
         opener.setNewPageStatus(true);
         opener.alertNewPageStatus();
         }
         window.onunload = function(){
         opener.setNewPageStatus(false);
         opener.alertNewPageStatus();
         }
         </script>
        </body>
    </html>
      

  3.   

    a.html:<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Insert title here</title>
        </head>
        <body>
         <p>This is a.html</p>
         <a href="b.html" target="_blank" class="link">link</a>
         <script>
         function checkNewPageStatus(windowObj){
         if(windowObj.closed){
         alert("窗口已关闭");
         }else{
         alert("窗口已打开");
         }
         }
         </script>
        </body>
    </html>
    b.html<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Insert title here</title>
        </head>
        <body>
         <p>This is b.html</p>
         <script>
         window.onload = function(){
         opener.checkNewPageStatus(this);
         }
         window.onunload = function(){
         opener.checkNewPageStatus(this);
         }
         </script>
        </body>
    </html>
      

  4.   

    上面有点问题,蛋疼。。
    a.html:<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Insert title here</title>
        </head>
        <body>
         <p>This is a.html</p>
         <a href="b.html" target="_blank" class="link">link</a>
         <script>
         function checkNewPageStatus(windowObj){
         setTimeout(function(){
         if(windowObj.closed){
         alert("窗口已关闭");
         }else{
         alert("窗口已打开");
         }
         }, 100);
         }
         </script>
        </body>
    </html>
    b.html:<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Insert title here</title>
        </head>
        <body>
         <p>This is b.html</p>
         <script>
        
         window.onload = function(){
         opener.checkNewPageStatus(this);
         }
         window.onunload = function(){
         opener.checkNewPageStatus(this);
         }
         </script>
        </body>
    </html>
      

  5.   

    window.parent.function(value)看你什么时候传 就什么时候调用父页面函数
      

  6.   

    1.在里面定义一个变量state 
       var state = false;2.在弹出b窗口的时候,设置state=true3.在b窗口里面加入事件:
    window.onunload=function(){
     opener.state = false;
    }
    取state就可以b的状态(开启/已关闭)