1. 程式人生 > >window.showModelessDialog和window.open

window.showModelessDialog和window.open

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

比如在父窗體parentForm裡面 通過 window.open(“subForm.html”),那麼在subform.html中 window.opener

就代表parentForm,可以通過這種方式設定父窗體的值或者呼叫js方法。

如:1,window.opener.test(); —呼叫父窗體中的test()方法

2,如果window.opener存在,設定parentForm中stockBox的值。

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

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

}

1>window.opener 的用法

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

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

2〉

在應用中有這樣一個情況,
在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();”;
}
}


上面的程式碼在關閉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();”;
}
}


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;
}
}
關於window.opener

window.opener 的用法

window.opener 返回的是建立當前視窗的那個視窗的引用,比如點選了a.htm上的一個連結而打開了b.htm,然後我們打算在b.htm上輸入一個值然後賦予a.htm上的一個id為“name”的textbox中,就可以寫為:

window.opener.document.getElementById("name").value = "輸入的資料";

對於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

按下列步驟執行:

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

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

其中先後這兩次開啟的子視窗的windowName一樣

此時你會發現在window2中的win.opener卻不是指向window2的,卻是指向window1.

如果你想在子視窗關閉父視窗的話,就不正確了,因此可以修改上面的執行方法為:

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方法的第二個引數傳遞window物件
比如: window.showModelessDialog(‘a.htm’,window);
2 在a.htm中就可以通過window.dialogArguments獲取該引數
比如: window.dialogArguments.fun1();
PS:子視窗可以通過設定window.returnValue設定頁面返回值
比如: window.returnValue=’OK’;window.close();

strRtn=window.showModalDialog(……)

這時,strRtn=’ok’

頁面中實現:
父頁面
function reloadPage() {
document.form1.submit();
}
彈出頁面呼叫closeWin();
function closeWin(){
hasClosed = true;
window.opener.location=”javascript:reloadPage();”;
window.opener=null;
window.close();
}

相關推薦

window.showModelessDialogwindow.open

window.opener 實際上就是通過window.open開啟的窗體的父窗體。 比如在父窗體parentForm裡面 通過 window.open(“subForm.html”),那麼在subform.html中 window.opener 就代表pa

window.openwindow.showModalDialogwindow.showModelessDialog 的區別

一、前言 要開啟一個可以載入頁面的子視窗有三種方法,分別是window.open、window.showModalDialog和window.showModelessDialog。 open方法就是開啟一個頁面,可以說同用url連結開啟一個頁面一樣,不推薦使用,因為

window.openerwindow.open的使用

菜單 參數 一行代碼 zab 高度 操作 opener ati ble window.opener和window.open的使用 window.opener是指調用window.open方法的窗口。window.opener 返回的是創建當前窗口的那個窗口的引用,比如點擊了

window.showModalDialogwindow.open的引數區別

window.showModalDialog(viewURL,window,"dialogHeight=400px;dialogWidth=600px;center=yes;status=no

window.locationwindow.open()的區別

      在給按鈕、表格、單元格、下拉列表和DIV等做連結時可以使用JS實現,和做普通連結一樣,可能我們需要讓連結頁面在當前視窗開啟,也可能需要在新視窗開啟,這時我們就可以使用下面兩項之一來完成:     window.open 用來開啟新視窗     window.l

js 中實現頁面跳轉的方法(window.locationwindow.open的區別)

<html> <head> <script language="javascript"><!-- function old_page() { window.location = "http:

javascript 開啟頁面window.locationwindow.open的區別

轉自:http://www.jb51.net/article/22616.htm 有時候需要用js來實現頁面的開啟,因為js下有window.location和window.open的不同實現方法,下面來簡單的說明下區別。 window.location = "http:/

window.locationwindow.open做連結的區別 //轉載

在給按鈕、表格、單元格、下拉列表和DIV等做連結時一般都要用Javascript來完成,和做普通連結一樣,可能我們需要讓連結頁面在當前視窗開啟,也可能需要在新視窗開啟,這時我們就可以使用下面兩項之一來完成: win

window.locationwindow.location.href區別詳解

window.location是一個物件,包含屬性有 hash 從井號 (#) 開始的 URL(錨) host 主機名和當前 URL 的埠號 hostname 當前 URL 的主機名 href 完整的 URL pathname 當前 URL 的路徑部分 port 當前

js中window.parentwindow.top

1.在使用iframe時,parent是父集視窗,top是頂級視窗;2.當自己是頂級視窗:    (1)則top返回的是自身;    (2)則parent:        ①當頂級視窗外沒有框架時,是自身;        ②當頂級視窗外還有框架,則還有父集。

js中window.openerwindow.parent的用法

在最近開發的web專案中,經常用到頁面中巢狀很多的頁面,頁面間的傳值。 現在總結如下: 1.window.self就表示當前開啟的視窗 2.window.top就表示最頂層的視窗(假如說你在一個窗口裡面有嵌套了其他一些視窗,那麼top就表示這個最頂層的視窗) 3.window.parent----是ifr

window.open()window.showModalDialog

iframe ngs 連接 and 瀏覽器中 指定 window esp tool 零、window.open()和window.showModalDialog 本人在使用時主要實現如下個功能,   以對話框形式彈出畫面,且要求對話框置頂,不可操作其他畫面,並且關閉畫面時刷

window.open()window.showModalDialog中參數傳遞

yslow dst 非模態 中文 jscript process left proc dsa 轉載地址:http://www.jb51.net/article/60507.htm 本文實例講述了js的window.showModalDialog及window.open用法。

window.openwindow.opener

har lock csrf views mode 第一個 action token div window.open是用來打開一個新窗口的;window.opener是調用父級窗口 舉個栗子: 1.index.html頁面: <!DOCTYPE html> &l

如何讓安卓WebView支援js呼叫window.open()window.close()的方法。

最近專案中遇到一個webview開發時的問題,web前端開發人員在網頁上實現了一個功能。功能是在A html頁面,開啟一個新的B html頁面,然後再B頁面中選擇一個聯絡人,把值返回給A頁面。做web開發的朋友應該知道,這個需要在A中通過window.open()開啟B,在

Webview 如何支援window.open window.close

WebSettings ws = mWebView.getSettings(); ws.setJavaScriptEnabled(true); ws.setJavaScriptCanOpenWindowsAutomatically(true); ws.setS

window.location.hrefwindow.open的幾種用法區別

使用js的同學一定知道js的location.href的作用是什麼,但是在js中關於location.href的用法究竟有哪幾種,究竟有哪些區別,估計很多人都不知道了。 回到頂部 一、location.href常見的幾種形式 目前在開發中經常要用

解決模式對話方塊window.open開啟新頁面Session會丟失問題

模式對話方塊頁面通過超連結(_blank類)或window.open開啟新頁面Session會丟失 解決方法如下: 在使用showModalDialog時會經常出現Session失效的問題,尤其是在這樣的使用情況下: 首先window. showModalDialog,然後

location.hrefwindow.open的幾種用法區別

一、location.href常見的幾種形式 self.location.href;//當前頁面開啟URL頁面 window.location.href;//當前頁面開啟URL頁面 this.location.href;//當前頁面開啟URL頁面 locati

JS中window.openwindow.opener的使用

BActionpublic void save(HttpServletRequest request,HttpServletResponse response) {  StduentSave();  PrintWriter pw = response.getWriter();  String jsAlert