2009-11-25 89 views
14

許多網站的腳本類似於下面的腳本。這些都是爲了阻止其他人構建網站。使用JS我怎麼能阻止子iframe從重定向或至少提示用戶重定向

if (top.location != location) { 
    top.location.href = document.location.href; 
} 

我需要一些方法來實現iframe要試圖重定向,如果是,我將刪除的iFrame,而是放置一個鏈接到該網站。這樣,我就不違反網頁框架的使用政策,並且鏈接到網站。我知道你可以使用討論herehere的onbeforeunload事件,但兩者看起來確實是不道德的。我記得在某處讀過一些完全相同的東西(digg做同樣的事情)。任何線索?

回答

16

的問題與上述方案是,它可以用一個簡單的去除:

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來完成,但通常會有點慢。如果可以,請使用服務器。

+1

第一個選項看起來不錯。雖然有一個問題,瀏覽器上按下的刷新將觸發window.onbeforeunload處理程序,並執行在函數中定義的完全相同的操作。此外,網站上的任何操作(如頁面請求或表單提交)都不會通過處理程序再次調用。對此的解決方案將是一個相當痛苦的「添加預先提交/導航的JavaScript功能,以取消框架腳本腳本的行動。」 – 2009-12-04 04:51:04

+0

這不會阻止某人使用' .. 。' – Andrew 2014-01-21 21:15:39

3

你可以嘗試創建框架剋星剋星,一切都是由傑夫·阿特伍德在他的博客中描述:We Done Been ... Framed! - coddinghorror.com

總之,你應該實現這樣的事情:

var prevent_bust = 0 
window.onbeforeunload = function() { prevent_bust++ } 
setInterval(function() { 
    if (prevent_bust > 0) { 
    prevent_bust -= 2 
    window.top.location = 'http://server-which-responds-with-204.com' 
    // replace your iframe with link to it 
    } 
}, 1) 
+0

看起來沒事。將不得不多寫一些js來執行所有其他操作。謝謝!! – 2009-11-26 11:30:51

6

如果您使用的是HTML5,我會建議使用新的「沙盒」屬性,但它尚未與所有瀏覽器兼容。 http://www.w3schools.com/tags/att_iframe_sandbox.asp

在我來說,我只是說「讓表單」和「允許腳本」,爲沙箱屬性值和嵌入式站點不再重定向和仍然可以運行使用「的JavaScript

允許頂部導航「因爲價值使網站重新導向了!

例如:​​

如果有人知道的任何缺點,使用這種方法,我很想知道。

別人用同樣的答案:https://stackoverflow.com/a/9880360/1404129(我就遇到了這個答案後)