2014-03-04 62 views
0

例如,我有一個名爲www.example.com的網站,它是一個父域。 我在裏面使用框架。在框架內,我打電話www.mail.example.com如何找到父域和頂級域(iframe)之間的區別來克服跨框架腳本

我需要檢查是否mail.example.com(裏面的iframe URL)是在谷歌有相同的域...

這可以防止跨框架腳本。 如何實現它?

情態動詞我工作了..

if (window.document.domain != top.document.domain) { 
    SWGuardianWindowOpen("http://google.com","_self",""); 
    } 

另:

if (self != top) 
{ 
//redirect to the google.com 
} 


if (parent && parent.frames && parent.frames.length>0) 

但這些都沒有幫助...

+0

請使用'example.com' /'example.net' /'example.org'來代替其他人運行的真實網站。 – Quentin

+0

https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage – Quentin

回答

1

你並不需要,如果你想防止黑客的JavaScript非嵌入您的網站的同源網頁。

只需將以下代碼添加到您的HTML文檔的<head>(例如:http://jsfiddle.net/b28CK/show/):

<meta http-equiv="X-Frame-Options" content="SAMEORIGIN"> 

如果你被允許修改服務器的響應頭,你還可以添加X-Frame-Options: SAMEORIGIN響應頭部來達到相同的效果。

如果您想使用JavaScript檢測父源,則可以使用location.ancestorOrigins找出父源的列表。但這隻適用於基於Webkit和Chromium的瀏覽器。

+0

你可以請示例location.ancestorOrigins嗎? –

+0

@Kumar它只是一個起源列表(即scheme:// host,例如'http:// example.com'),從父框架開始到頂部框架。使用for循環枚舉列表,例如'var origins = location.ancestorOrigins; for(var i = 0; i