176142998

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks

          window.opener 實際上就是通過window.open打開的窗體的父窗體。

          比如在父窗體parentForm里面 通過 window.open("subForm.html"),那么在subform.html中 window.opener

          就代表parentForm,可以通過這種方式設(shè)置父窗體的值或者調(diào)用js方法。

          如:1,window.opener.test(); ---調(diào)用父窗體中的test()方法

              2,如果window.opener存在,設(shè)置parentForm中stockBox的值。

              if (window.opener && !window.opener.closed) {

                 window.opener.document.parentForm.stockBox.value = symbol;

          }

          1>window.opener 的用法

          在一般的用法中,只是用來解決關(guān)閉窗口時不提示彈出窗口,   而對它更深層的了解一般比較少。其   實   window.opener是指調(diào)用window.open方法的窗口。
               在工作中主要是用來解決部分提交的。這種跨頁操作對工作是非常有幫助的。
          如果你在主窗口打開了一個頁面,并且希望主窗口刷新就用這個,打開頁面的window.opener就相當(dāng)于
          主窗口的window。
          主窗口的刷新你可以用
          window.opener.location.reload();
          如果你用虛擬的目錄:如struts的*.do會提示你重試

          你可以改成這樣 window.opener.yourformname.submit()
          就好了

          2〉

          在應(yīng)用中有這樣一個情況,
          在A窗口中打開B窗口,在B窗口中操作完以后關(guān)閉B窗口,同時自動刷新A窗口


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

          </script>
          上面的代碼在關(guān)閉B窗口的時候會提示錯誤,說缺少Object,正確的代碼如下:
          function closeWin(){
                   hasClosed = true;
                   window.opener.location="javascript:reloadPage();";
                   window.opener=null;
                   window.close();
               }
               function window.onbeforeunload(){
                   if(!hasClosed){//如果已經(jīng)執(zhí)行了closeWin方法,則不執(zhí)行本方法
                       window.opener.location="javascript:reloadPage();";
                   }
               }

          </script>
          reloadPage方法如下:
          function reloadPage() {
                   history.go(0);
                   document.execCommand("refresh")
                   document.location = document.location;
                   document.location.reload();
               }
          PS:由于需要支持正常關(guān)閉和強(qiáng)制關(guān)閉窗口時能捕捉到事件,用了全局變量hasClosed

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

          補(bǔ)充,在父窗口是frame的時候在刷新父窗口的時候會出現(xiàn)問題:

          The page cannot be refreshed without resending the information.
          后修改如下:
          window.opener.parent.document.frames.item('mainFrame').location.href = window.opener.location.href;
          不需要執(zhí)行自帶的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;
                   }
               }

          關(guān)于window.opener

          window.opener 的用法

              window.opener 返回的是創(chuàng)建當(dāng)前窗口的那個窗口的引用,比如點(diǎn)擊了a.htm上的一個鏈接而打開了b.htm,然后我們打算在b.htm上輸入一個值然后賦予a.htm上的一個id為“name”的textbox中,就可以寫為:

              window.opener.document.getElementById("name").value = "輸入的數(shù)據(jù)";

              對于javascrīpt中的window.opener沒有很好的理解。

              為什么框架中不能使用,彈出窗口的父窗口不能在框架里面的某個頁面呢?那怎樣通過彈出窗口操作框架中的父窗口呢?

              opener.parent.frames['frameName'].document.all.input1.value 試試這個:)

          正確使用window.open返回對象的opener

           

          眾所周知JavaScript中:

          var win = window.open(url,windowName,...); 的使用,

          而win.opener則是指向父窗口的引用

          然而,有種情況卻比較特別,

          假如有兩個窗口window1和window2

          按下列步驟執(zhí)行:

          var win = window.open(url,windowName,...);// (window1)

          var win = window.open(url,windowName,...);//(window2)

          其中先后這兩次打開的子窗口的windowName一樣

          此時你會發(fā)現(xiàn)在window2中的win.opener卻不是指向window2的,卻是指向window1.

          如果你想在子窗口關(guān)閉父窗口的話,就不正確了,因此可以修改上面的執(zhí)行方法為:

          var win = window.open(url,windowName,...);? (window1)

          win.opener = window;

          var win = window.open(url,windowName,...);? (window2)

          win.opener = window;

          只有這樣修改才OK

           

           

           

          通過window.showModalDialog或者.showModelessDialog彈出的頁面

          這種情況需要兩個步驟:
          1 在父窗口.showModalDialog或.showModelessDialog方法的第二個參數(shù)傳遞window對象
          比如: window.showModelessDialog('a.htm',window);
          2 在a.htm中就可以通過window.dialogArguments獲取該參數(shù)
          比如: window.dialogArguments.fun1();
          PS:子窗口可以通過設(shè)置window.returnValue設(shè)置頁面返回值

          比如: window.returnValue=OK;window.close();

          strRtn=window.showModalDialog(......)

          這時,strRtn='ok'


          頁面中實現(xiàn):
          父頁面
          function reloadPage() {
                   document.form1.submit();
               }
          彈出頁面調(diào)用closeWin();
          function closeWin(){
                   hasClosed = true;
                   window.opener.location="javascript:reloadPage();";
                   window.opener=null;
                   window.close();
               }
          posted on 2008-08-05 16:05 飛飛 閱讀(494) 評論(1)  編輯  收藏

          Feedback

          # re: window.opener用法 2008-11-20 14:43
          謝謝~轉(zhuǎn)了  回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 蓬莱市| 邳州市| 萍乡市| 微博| 永嘉县| 荔波县| 南漳县| 新龙县| 驻马店市| 平远县| 淮北市| 海城市| 双流县| 明光市| 宜宾市| 湖州市| 麦盖提县| 恩施市| 玉田县| 遵义县| 柳河县| 潮州市| 嵩明县| 东乌珠穆沁旗| 沽源县| 扬中市| 永修县| 横峰县| 吉首市| 保康县| 云南省| 信宜市| 曲靖市| 永兴县| 康定县| 德州市| 龙海市| 台湾省| 六盘水市| 长岛县| 扶余县|