的問題與上述方案是,它可以用一個簡單的去除:
if(top != self)
delete top.onbeforeunload;
一旦被調用,那麼prevent_bust
將ñ永遠不會增加,這意味着網站將在沒有您的同意或知識的情況下自由重新定向。糟糕的交易。
如果你想該解決方案的一貫功能版本,我建議這樣做,而不是:
// Create a random seed value, making it almost impossible to
// determine what is being tested for.
var prevent_bust = Math.random() * 3000;
// enclose everything in a function, so that it cannot be addressed
function iniFunc (init) {
// The function is no longer in scope of the main window.
function onbeforeunload() { prevent_bust++ }
window.onbeforeunload = onbeforeunload;
setInterval(function() {
// make sure the function was not deleted.
if(window.onbeforeunload != onbeforeunload)
{
prevent_bust = init + 1;
window.onbeforeunload = onbeforeunload;
}
if (prevent_bust > init) { // All comparison is to the random seed.
prevent_bust -= 2
window.top.location = 'http://server-which-responds-with-204.com/'
// Unfortunately, you have absolutely no idea which website caused
// the incrementation, so you cannot replace it with a link!
//
// You might try to simply ignore it and just use the iframe as is --
// theoretically, they are no longer able to bust this frame.
// (this theory will be disproved below).
}
}, 1);
};
iniFunc(prevent_bust);
不幸的是,這也導致留下一個問題 - 它是檢索它具有間隔件小事設置,取消設置,然後重新定向頁:
// setTimeout will return the highest timeout which is not "in use", in this case,
// it will be the original setInterval (from the above function) + 1.
// Event if there are 1,000 intervals already set, it will be rather trivial to
// clear them all.
var currentInterval = 10000;
// window.setTimeout(gotoHREF, 100);
// clearInterval will not interfere with setTimeout, so we can clear all
// of the Intervals already set.
for(var i = 0; i < currentInterval; i++) top.clearInterval(i);
function gotoHREF(){
top.location.href = "http://<my-url/>";
}
你最好的選擇實際上是解決這個問題的服務器端(如果可以的話)。如果你有機會到服務器的網站,該網站將舉行內部框架,創建一個臨時,代理位置,可以在網站的數據拉,然後剝去腳本標籤:
// In php
$dd = new DOMDocument();
// file_get_contents will simply convert the entire web address into a String
$dd->loadXML(file_get_contents("http://" . $_GET[ 'loadedURL' ]));
$scripts = $dd->getElementsByTagName("script");
// iterate through the website and remove all script tags.
for($i = 0; $i < $scripts->length; $i++)
{
$current = $scripts->item($i);
$current->parentNode->removeChild($current);
}
// output it to the dummy page.
echo $dd->saveXML();
然後,您可以使用標籤:
<iframe src="redirect.php?loadedURL=http://www.google.com"></iframe>
不幸的是,這將意味着您的iframe將在沒有JavaScript的情況下運行,如果沒有完全激活相關網站,可能會造成癱瘓。您還需要確保所有src屬性都已正確修改爲外部網站HTML中的後代節點。另一方面,您可以讓服務器檢查框架站點及其所有JS頁面,以查看RegExp的頂部(。| [\ s *(「|'))位置(這將與頂部匹配。位置,頂部[「位置」和頂部[位置])(或者如果有任何對頂部的引用),然後使用鏈接(如果它存在)和站點本身(如果不存在)。這裏的不利之處在於,您迫使用戶等待輔助站點加載兩次 - 一次在服務器上,一次在瀏覽器中。 (除非所有事情都是通過JS完成的,但在我看來,這通常更令人討厭)。
就我個人而言,我認爲「不架我的網站」的人羣通常可以直接贏得大部分涉及iframe的戰鬥。另一方面,如果在將HTML附加到網頁之前使用代碼來處理HTML,則另一方不僅僅是一個戰鬥機會。
作爲一個側面說明,這些都可以通過JavaScript和AJAX來完成,但通常會有點慢。如果可以,請使用服務器。
第一個選項看起來不錯。雖然有一個問題,瀏覽器上按下的刷新將觸發window.onbeforeunload處理程序,並執行在函數中定義的完全相同的操作。此外,網站上的任何操作(如頁面請求或表單提交)都不會通過處理程序再次調用。對此的解決方案將是一個相當痛苦的「添加預先提交/導航的JavaScript功能,以取消框架腳本腳本的行動。」 – 2009-12-04 04:51:04
這不會阻止某人使用'
.. 。' – Andrew 2014-01-21 21:15:39