天天看點

js實作網頁防止被iframe架構嵌套及幾種location.href的差別

首先我們了解一下幾種location.href的差別簡單的說:幾種location.href的差別js實作網頁被iframe架構功能,感興趣的朋友可以了解下

首先我們了解一下:window.location.href、location.href、self.location.href、parent.location.href、top.location.href他們的差別與聯系,簡單的說:幾種location.href的差別 js實作網頁被iframe架構功能 

"window.location.href"、"location.href"、"self.location.href"是本頁面跳轉 

"parent.location.href"是上一層頁面跳轉 

"top.location.href"是最外層的頁面跳轉 

舉個例子說明(如上圖): 

如果A,B,C,D都是普通頁面,D是C的iframe,C是B的iframe,B是A的iframe, 

如果D中js這樣寫: 

"window.location.href"、"location.href":D頁面跳轉 

"parent.location.href":C頁面跳轉 

"top.location.href":A頁面跳轉 

如果D頁面中有form的話: 

<form>: form送出後D頁面跳轉 

<form target="_blank">: form送出後彈出新頁面 

<form target="_parent">: form送出後C頁面跳轉 

<form target="_top"> : form送出後A頁面跳轉 

關于頁面重新整理,D 頁面中這樣寫: 

"parent.location.reload();": C頁面重新整理 (當然,也可以使用子視窗的 opener 對象來獲得父視窗的對象:window.opener.document.location.reload(); ) 

"top.location.reload();": A頁面重新整理 

網頁防止被架構方法代碼: 

<script language="javascript"> 

if(top.location!==self.location){ 

WarningTxt1 = "content頁面被iframe了!"; 

WarningTxt2 = "我們跳出iframe,直接通路content頁面吧!"; 

alert(WarningTxt1); 

alert(WarningTxt2); 

top.location.href=self.location.href; 

</script> 

本文轉自左正部落格園部落格,原文連結:http://www.cnblogs.com/soundcode/p/3955369.html,如需轉載請自行聯系原作者

繼續閱讀