Isa*_*vin 5 javascript window.location window.opener
我找了一会儿,找不到符合我需要的答案.我有一个页面弹出一个窗口(window.open),记录用户(创建一个cookie,设置会话)然后重定向到另一个页面.虽然modal是重定向的,但我想刷新父页面,所以我刚才所做的所有好东西都会被父级识别.我试过window.opener和类似的东西.有人可以帮我一点吗?谢谢
T.J*_*der 10
window.opener在弹出窗口中会引用window打开窗口的对象,所以当然你应该可以调用window.opener.location.reload();.如果您没有违反同源策略,弹出窗口可以调用脚本并操纵父窗口对象上的属性,就像父窗口中的代码一样.
这是一个实例,演示了孩子回调父母的一个函数,并直接操纵父母.以下引用了完整的代码.
但是那个例子并没有完全按照你说的去做,所以我也做了这个,让孩子刷新父页面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>?
Run Code Online (Sandbox Code Playgroud)
(你不会有像我上面那样使用范围界定功能,但要保持你的全局变量包含的是有用的.)
第一个实例的子代码:
<!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>?
Run Code Online (Sandbox Code Playgroud)