`

windows.opener

阅读更多

      window.opener.location.reload();这个方法在强迫父窗口的时候,在有些IE浏览器(比如安全设置高)的情况下,会弹出一个确认对话框,提示是不是要重新再刷新一次页面,这可是比较郁闷的事情哦,我后来把这个方法替换成了window.opener.location.href=window.opener.location.href;
就不会出现那样的问题了。

       window.opener其实是指本窗口的父窗口,比如,one.jsp 通过popupwindow打开了two.jsp,哪么在two.jsp里面的window.opener就是指one.jsp,所以在two.jsp里面完全可以用window.opener调用任何一个one.jsp里面的方法,实现one.jsp和two.jsp的交互。
       注意:window.opener.location.href只是一个链接,如果想实现父窗口的提交就要调用window.opener.action="" 和window.opener.submit();方法,但是不幸的是这段代码在firefox下不能运行,解决的办法为在父窗口中写一个提交的function在子窗口中通过window.opener.functionname()调用。
       通常在使用window.opener的时候要去判断父窗口的状态,如果父窗口被关闭或者更新,就会出错,解决办法是加上如下的验证if(window.opener && !window.opener.closed)
有时我们需要在新打开的窗口里面编辑信息,等编辑完了,需要将当前窗口关闭并且刷新父窗口,以使修改生效,本文就是介绍用 javascript 来实现"更新记录后关闭子窗口并刷新父窗口".


1. <script language="JavaScript" type="text/javascript">
2. <!--
3. function refreshParent()
4. {
5.     window.opener.location.href = window.opener.location.href;
6.     if (window.opener.progressWindow)
7.     {
8.         window.opener.progressWindow.close();
9.     }
10.     window.close();
11. }
12. //-->
13. </script>
14.
15. <a href="javascript:void(0)" onclick="refreshParent()">刷新父窗口并关闭当前窗口</a>
16.


1:   window.parent 是iframe页面调用父页面对象

举例:
a.html
17. 程序代码
18. <html>
<head><title>父页面</title></head>
<body>
<form name="form1" id="form1">

<input type="text" name="username" id="username"/>

</form>
<iframe src="b.html" width=100%></iframe>
</body>
</html>
19.

如果我们需要在b.htm中要对a.htm中的username文本框赋值,就如很多上传功能,上传功能页在Ifrmae中,上传成功后把上传后的路径放入父页面的文本框中

我们应该在b.html中写
20. 程序代码
21. <script type="text/javascript">
var _parentWin = window.parent ;
_parentWin.form1.username.value = "xxxx" ;
</script>
22.

实例地址:  http://www.cnspry.cn/blog/attachments/window.parent实例/a.html

2:   window.opener 是window.open 打开的子页面调用父页面对象

a.html
23. 程序代码
24.
<script type="text/javascript">
function openSubWin()
{
var _width = 300 ;
var _height = 200 ;
var _left = (screen.width - _width) / 2 ;
var _top = (screen.height - _height) / 2 ;
window.open("b.html",null,
"height=" + _height + ",width=" + _width + ",status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=" + _left + ",top=" + _top);
}
</script>
<input type="text" name="username" id="username"/>
<input type="button" value="弹出子页面" onClick="openSubWin();">
25.

b.html
26. 程序代码
27.
<script type="text/javascript">
function UpdateParent()
{
var _parentWin = window.opener ;
_parentWin.form1.username.value = "xxxx" ;
}
</script>
<input type="button" name="button" id="button" value="更新主页面的UserName内容" onClick="UpdateParent();">

分享到:
评论

相关推荐

    Javascript中封装window.open解决不兼容问题

    对window.open进行封装, 使其更好用, 且更兼容, 很多人说window.open不兼容,其实不是, 因为不能直接执行, 必须通过用户手动触发才行;看代码: 代码如下 var openWindow = function(url, options) { ...

    用window.open,opener实现网页间通信

    var txt = window.opener.document.getElementById("msg"); txt.value = "secondsaysyouwelcome"; }); } function secondcall(msg) { if (msg.indexOf("third") &gt; 0) { tw = window.open("third.html", ...

    js弹窗并返回值(window.open方式)

    window.opener.result = result; window.close(); } // 用户在b.html操作后调用sendResultAndClose sendResultAndClose('用户的选择'); // 在原窗口监听新窗口关闭事件 window.addEventListener('message', ...

    asp.net后台关闭当前页面并传值的两种方法

    Response.Write("&lt;script&gt;windows.opener.document.all.TextBox1.value='" + value + "';opener=null;self.close();&lt;/script&gt;"); ``` 在上述代码中,假设父页面中有一个input或者textarea元素的id是TextBox1,那么...

    IE关闭主窗口时,同时关闭所有的子窗口

    在JavaScript中,`window.opener`属性可以返回打开当前窗口的窗口对象,而`window.open()`返回的新窗口对象则包含了`window`属性,形成一个树状结构。遍历这个结构,我们可以找到所有子窗口。 ```javascript ...

    js弹窗返回值详解(window.open方式)

    var fWindowText1 = window.opener.document.getElementById("text1"); fWindowText1.value = fWindowText1.value + "mayongzhan"; window.close(); } } ``` 在这个例子中,`window.opener`属性用于获取创建新...

    winmail_opener

    在压缩包内的文件名列表中,我们看到"winmail_opener.exe"可能是一个可执行文件,代表winmail_opener软件的主程序,用户可以通过双击运行来启动工具。而"Readme-说明.htm"则很可能包含有关如何使用这个工具的详细...

    showModalDialog在谷歌浏览器下会返回Null的解决方法

    对于 Chrome,我们使用 `window.opener.returnValue` 设置返回值,并调用 `window.opener.close()` 来关闭对话框。对于其他浏览器,我们使用 `window.returnValue`。 请注意,由于 `showModalDialog` 在现代浏览器...

    OpENer:OpENer是用于IO适配器设备的EtherNetIP堆栈。 它支持多个IO和显式连接,并包括对象和服务,这些对象和服务用于制造ODVA规范中定义的符合EtherNetIP的产品

    要测试此版本,我们建议安装了Cygwin( )Linux PC或Windows。 您将需要安装以下软件: CMake的海湾合作委员会使Binutils libcap的开发库(libcap-dev或等效的) 用于普通建筑。 这些应该安装在大多数L

    用javascript关闭本窗口技巧小结_.docx

    用户需要在地址栏输入`about:config`,然后找到`dom.allow_scripts_to_close_windows`这一项,并将其值设置为`true`。一旦设置完毕,JavaScript的`window.close()`方法就可以在Firefox中正常工作了。 但这种方法对...

    OpENer-master:EtherNetIP 驱动程序

    要测试此版本,我们建议使用安装了 Cygwin ( ) 的 Linux PC 或 Windows。 您将需要安装以下内容: 制作 海湾合作委员会 制作 二进制实用程序 对于普通建筑。 这些应该安装在大多数 Linux 安装上,并且是 Cygwin ...

    window.close(); 关闭浏览器窗口js代码的总结介绍

    - **Firefox**:对于用户直接输入URL或通过Ctrl+点击链接打开的窗口,`window.close()`可能不会生效,除非在`about:config`中调整`dom.allow_scripts_to_close_windows`设置为`true`。 - **Safari**:通常不允许`...

    windows和document方法详解.doc

    - `opener`: 代表使用`window.open`打开当前窗口的脚本所在窗口。 - `outerWidth`: 窗口的外部宽度。 - `outerHeight`: 窗口的外部高度。 - `pageXOffset`: 指定文档左上角在窗口中的水平偏移量。 - `...

    Python常用的爬虫技巧总结.doc

    opener = urllib2.build_opener(proxy) urllib2.install_opener(opener) response = urllib2.urlopen('http://www.baidu.com') print response.read() ``` 3. **处理Cookies** - 对于需要保持登录状态或跟踪...

    exampleJS:JavaScript示例代码

    exampleJS JavaScript示例代码 tryto_WebSocket_server_push WebSocket示例。服务器端是用C语言创建的。 使用libwebsockets库... example1在Windows 7 Chrome Ver.44中,该值未从子窗口传递到父窗口。 example2即使

    WebBrowser控件应用:弹出新窗体(源码)

    newWebBrowser.Navigate(webBrowser1.Document.Window.Opener.Url); newForm.Controls.Add(newWebBrowser); newForm.Show(); e.Cancel = true; // 阻止原生的新窗口弹出 } } ``` 在上面的代码中,`WebBrowser1...

    python urllib2模块实例简介.docx

    response = opener.open('http://example.com/') content = response.read() print(content) ``` ##### 2. 使用代理服务器 ```python import urllib2 proxy_handler = urllib2.ProxyHandler({ '...

    js关闭火狐浏览器窗口

    在配置列表中找到`dom.allow_scripts_to_close_windows`这个配置项是实现使用JavaScript关闭窗口的关键。此配置项用于控制是否允许JavaScript脚本执行`window.close()`方法来关闭窗口。 - **默认值**:此配置项的...

    在firefox和Chrome下关闭浏览器窗口无效的解决方法

    原因在于: Firefox下默认设置是无法通过脚本来关闭浏览器窗口的,为的是防止恶意脚本注入, 所以调整的方式就是在url地址栏中输入about:config, 然后在配置列表中找到 dom.allow_scripts_to_close_windows 点右键...

    js实现不提示直接关闭网页窗口

    总的来说,通过理解和应用JavaScript中的 `window.opener`, `window.open`, 和 `window.close` 方法,我们可以实现不提示用户直接关闭网页窗口的需求。但在实际开发中,要确保遵循最佳实践和浏览器的安全限制,以...

Global site tag (gtag.js) - Google Analytics