每日一得

          不求多得,只求一得 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 閱讀(3756) 評論(0)  編輯  收藏 所屬分類: web技術
          主站蜘蛛池模板: 琼海市| 德令哈市| 扶余县| 屏边| 铁力市| 莱西市| 泗洪县| 微博| 班玛县| 安宁市| 寻甸| 资兴市| 深水埗区| 确山县| 凯里市| 化隆| 辽源市| 新乡县| 罗城| 阜康市| 霍州市| 赞皇县| 江源县| 黑龙江省| 合江县| 内丘县| 清徐县| 安国市| 社会| 新和县| 三亚市| 新巴尔虎右旗| 民勤县| 崇信县| 平果县| 柏乡县| 宜宾县| 天全县| 五台县| 阜阳市| 札达县|