每日一得

不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速开发
最近关心的内容:SSH,seam,flex,敏捷,TDD
本站的官方站点是:颠覆软件

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  220 随笔 :: 9 文章 :: 421 评论 :: 0 Trackbacks
在应用中有这样一个情况,
在A窗口中打开B窗口,在B窗口中操作完以后关闭B窗口,同时自动刷新A窗口


function closeWin(){
        hasClosed 
= true;
        window.opener.location
="javascript:reloadPage();";
        window.close();
    }
    
function window.onbeforeunload(){
        
if(!hasClosed){
            window.opener.location
="javascript:reloadPage();";
        }
    }

</script>

上面的代码在关闭B窗口的时候会提示错误,说缺少Object,正确的代码如下:
function closeWin(){
        hasClosed 
= true;
        window.opener.location
="javascript:reloadPage();";
        window.opener
=null;
        window.close();
    }
    
function window.onbeforeunload(){
        
if(!hasClosed){//如果已经执行了closeWin方法,则不执行本方法
            window.opener.location
="javascript:reloadPage();";
        }
    }

</script>

reloadPage方法如下:
function reloadPage() {
        history.go(
0);
        document.execCommand(
"refresh")
        document.location 
= document.location;
        document.location.reload();
    }

PS:由于需要支持正常关闭和强制关闭窗口时能捕捉到事件,用了全局变量hasClosed

==============================================

补充,在父窗口是frame的时候在刷新父窗口的时候会出现问题:

The page cannot be refreshed without resending the information.

后修改如下:
window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;

不需要执行自带的reload()方法,注意,不要再画蛇添足加上这一句:

window.opener.parent.document.frames.item('mainFrame').location.reload();

========================================================================================
最后,为了同时支持刷新普通父窗口和frame父窗口,代码如下:
function closeWin() {
        hasClosed 
= true;
    
<%if(null != frame){%>
        window.opener.parent.document.frames.item('mainFrame').location.href 
= window.opener.location.href;
    
<%}else{%>
        window.opener.location 
= "javascript:reloadPage();";
    
<%}%>
        
//window.opener.top.mainFrame.location="javascript:reloadPage();";
        //self.opener.frames.mainFrame.location.reload(true);
        window.opener = null;
        window.close();
    }
    
function window.onbeforeunload(){
        
if (!hasClosed) {
        
<%if(null != frame){%>
            window.opener.parent.document.frames.item('mainFrame').location.href 
= window.opener.location.href;
        
<%}else{%>
            window.opener.location 
= "javascript:reloadPage();";
        
<%}%>
            window.opener 
= null;
        }
    }




posted on 2006-08-23 10:54 Alex 阅读(3737) 评论(0)  编辑  收藏 所属分类: web技术

只有注册用户登录后才能发表评论。


网站导航: