gpt4 book ai didi

javascript - 从javascript中的子窗口刷新父窗口

转载 作者:数据小太阳 更新时间:2023-10-29 05:51:02 25 4
gpt4 key购买 nike

我找了一段时间,找不到符合我需要的答案。我有一个弹出窗口 (window.open) 的页面,让用户登录(创建 cookie,设置 session ),然后重定向到另一个页面。当模态重定向时,我想刷新父页面,这样我刚刚所做的所有好事都会被父页面识别。我试过 window.opener 之类的东西。有人可以帮我一下吗?谢谢

最佳答案

window.opener在弹出窗口中将引用打开窗口的 window 对象,因此您当然应该能够调用 window.opener.location.reload();。只要你不违反 Same Origin Policy ,弹出窗口可以调用脚本并操作父窗口对象的属性,就像父窗口中的代码一样。

这是一个 live example演示子级回调父级的函数,并直接操纵父级。下面引用了完整的代码。

但是那个例子没有完全你说的,所以我done this one as well ,它让 child 通过 window.opener.location.reload() 刷新父页面。

第一个实例的父代码:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Parent Window</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<input type='button' id='btnOpen' value='Open Window'>
<div id='display'></div>
</body>
<script type='text/javascript'>
(function() {
var btnOpen = document.getElementById('btnOpen');
btnOpen.onclick = btnOpenClick;

function btnOpenClick() {
window.open('http://jsbin.com/ehupo4/2');
}

// Make the callback function available on our
// `window` object. I'm doing this explicitly
// here because I prefer to export any globals
// I'm going to create explicitly, but if you
// just declare a function in a script block
// and *not* inside another function, that will
// automatically become a property of `window`
window.callback = childCallback;
function childCallback() {
document.getElementById('display').innerHTML =
'Got callback from child window at ' + new Date();
}
})();
</script>
</html>​

(您不必像我上面那样必须使用作用域函数,但它有助于保持全局变量的包含。)

第一个实例的子代码:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Child Window</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<p>I'm the child window</p>
</body>
<script type='text/javascript'>
if (window.opener) {
// Call the provided callback
window.opener.callback();

// Do something directly
var p = window.opener.document.createElement('p');
p.innerHTML = "I was added by the child directly.";
window.opener.document.body.appendChild(p);
}
</script>
</html>​

关于javascript - 从javascript中的子窗口刷新父窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3762329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com