しばらく探しましたが、自分のニーズに合った答えが見つかりません。ウィンドウをポップし(window.open)、ユーザーをログインさせ(Cookieを作成し、セッションを設定)、別のページにリダイレクトするページがあります。モーダルがリダイレクトしている間に、親ページを更新したいので、行ったすべての良いことは親によって認識されます。私はwindow.openerなどを試しました。誰かが私を少し助けてくれますか?ありがとう
4 に答える
window.opener
ポップアップ内のwindow
は、開いているウィンドウのオブジェクトを参照するので、もちろん を呼び出すことができるはずですwindow.opener.location.reload();
。Same Origin Policyに違反しない限り、ポップアップ ウィンドウは、親ウィンドウ オブジェクトのコードと同じように、スクリプトを呼び出して親ウィンドウ オブジェクトのプロパティを操作できます。
これは、子が親の関数にコールバックし、親を直接操作する実例です。この完全なコードを以下に引用します。
しかし、その例はあなたが言ったことを正確に実行しなかったので、これも実行しました。これにより、子は を介して親ページを更新します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>
他の提案がどれも機能しない場合(すべての主要なブラウザーでテストすることを忘れないでください)、親ウィンドウの参照を子ウィンドウに明示的に渡すこともできます。子ウィンドウへの参照を返す必要があるためwindow.open()
です。そうすれば child = window.open()
、次のようなことができます。child.myParent = window
window.parent.top.location = window.parent.top.location; (またはそれに似たものがそれを行います)