2016-04-15 116 views
0

我有一種情況,我創建了一個網站,客戶不希望其他開發人員能夠使用iframe,但是當有人使用iframe時,它會將用戶重定向到iframe試圖通過的網站。從Iframe內部加載站點時重定向頁面?

我一直在尋找代碼,這將允許我檢測客戶端站點是否加載到iframe中,然後將其重定向回該站點。對於JS現場相當新穎,我不能100%確定如何去做這件事。

到目前爲止,我有將被放入的document.ready功能

var url = "http://google.com"; 
$(location).attr('href',url); 

所以,現在的問題進來是利用通過檢測另一個開發人員在一個iframe使用我的網站重定向的重定向代碼在另一個網站上。

在此先感謝!

+0

https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options – Prinzhorn

回答

1

從iframe中跳出來,你可以做這樣的事情:

// if the location of the top (topmost browser window) not equal to the current location then it redirects to the URL of the current window (iframe) 
if (window.top.location != document.location) { 
    window.top.location.href = document.location.href; 
} 
+0

「頂.location「,這個檢測到底是什麼? – factordog

+0

哦,等我明白了。所以邏輯是,它將檢查它在url上的實際頁面,然後如果它不等於我的客戶端的指定站點,它將重定向? – factordog

+0

美麗。完美的作品 – factordog