簡體   English   中英

從 iFrame 重定向父窗口在 Safari 中不起作用

[英]Redirect parent window from iFrame not working in Safari

我的網站是 mysite.com。 我正在 iframe othersite.com 並讓我的用戶完成一項任務。

我可以通過 API 檢查我的用戶是否在 othersite.com 上完成了任務。 但是,我想找出另一種方法來確定用戶何時完成了任務,以便我可以導航到 mysite.com 上的下一頁。

當用戶在 othersite.com 上完成任務時,othersite.com 會將他們重定向到 iframe 內的mysite.com/complete/ 我一直在利用這一點,將以下 JS 代碼放在mysite.com/complete/

window.top.location == 'mysite.com/next';

使父窗口自動推送到下一頁。 我在 Chrome 上有這個工作,但在 Safari 上它重定向內部 iframe,而不是父級。

什么可能導致 Chrome 和 Safari 之間的差異? 有沒有更好的方法來實現這個目標?

注意:我已經看到了這個答案,所以 Safari 中的window.top.location函數似乎發生了一些事情,但即使嘗試修復,我也無法轉義內部 iframe。

31

你不能那樣做。 大多數瀏覽器都不允許跨站點腳本。

但是,您可以通過此處描述的跨文檔消息傳遞與另一個窗口進行通信: https : //developer.mozilla.org/en/DOM/window.postMessage

您最多可以從彈出窗口向 opener 發送消息,並在 opener 中收聽此類消息。 然后開瓶器必須自己改變它的位置。

父窗口:

<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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM