[英]window.opener.location.href works in IE but not Chrome or Safari
我一直在研究這個問題,雖然在各種論壇上有很多關於類似問題的帖子,但沒有一個問題或解決方案與我的完全匹配。
我有一個應用程序已成功使用下面的函數在完成彈出窗口后重定向回父窗口。 最近我一直在研究與其他瀏覽器的兼容性(允許系統通過iPad使用),發現在使用Safari或Chrome時該功能存在問題。
父頁面是一些數據庫信息的摘要,用戶單擊鏈接打開一個窗口(通過 window.open)以查看更詳細的數據。 完成后,子窗口上有一個鏈接可以刷新父窗口上的數據(部分是為了確保返回父窗口時顯示正確的數據)並關閉子窗口。
Safari 中的控制台報告“'window.opener.location.href' 的結果不是函數”。 我曾嘗試使用上面的“window.opener.document.location.href”和“window.opener.window.location.href”(取自網上提供的其他解決方案),但沒有成功。
我知道有些人的這個功能運行良好,而其他人則有這種問題。 我想知道這種特定情況是否有任何答案。
這是我的功能:
function quicklink(url) {
window.opener.document.location.href(url);
self.close();
}
這從第一天開始就在 IE7、8 和 9 上起作用,但在 Safari(適用於 Windows 或 iPad)或 Chrome 中不起作用。
有任何想法嗎?
href
是一個屬性,而不是一個方法。 只需將 URL 分配給它:
window.opener.document.location.href = url;
這也適用於 IE。 它也是那里的一個屬性,即使它允許您將其用作方法。
使用下面的代碼來達到預期的結果。
家長:
<script language="javascript">
function open_a_window()
{
var w = 200;
var h = 200;
var left = Number((screen.width/2)-(w/2));
var tops = Number((screen.height/2)-(h/2));
window.open("window_to_close.html", '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+tops+', left='+left);
return false;
}
// opener:
window.onmessage = function (e) {
if (e.data === 'location') {
window.location.replace('https://www.google.com');
}
};
</script>
<input type="button" onclick="return open_a_window();" value="Open New Window/Tab" />
彈出:
<!DOCTYPE html>
<html>
<body onload="quitBox('quit');">
<h1>The window closer:</h1>
<input type="button" onclick="return quitBox('quit');" value="Close This Window/Tab" />
<script language="javascript">
function quitBox(cmd)
{
if (cmd=='quit')
{
window.opener.postMessage('location', '*');
window.open(location, '_self').close();
}
return false;
}
</script>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.