首先咱們瞭解一下幾種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頁面刷新
如今回頭看看,js實現網頁防止被iframe框架功能就很簡單了。假設frame.html文件中框架了content.html文件,那麼思路是這樣的:在content.html中加入js檢測本身自己top.location.href地址,是否爲top.location.href地址。若是是則沒被嵌套,若是否的話即被嵌套了,這是咱們能夠提示一下。
<head>
<script language="javascript">
if(top.location!=self.location){
top.location=self.location;
}
</head>