国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

js實現網頁防止被iframe框架嵌套及幾種location.href的區別

2024-09-06 12:41:26
字體:
來源:轉載
供稿:網友
首先我們了解一下: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地址。如果是則沒被嵌套,如果否的話即被嵌套了,這是我們可以提示一下。預覽效果

網頁防止被框架方法代碼:
代碼如下:
<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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洛南县| 黄陵县| 津南区| 新晃| 江西省| 类乌齐县| 清涧县| 东方市| 海安县| 金堂县| 綦江县| 昌平区| 辽源市| 应城市| 双流县| 竹山县| 蒙阴县| 宣威市| 太康县| 古田县| 赤峰市| 澄城县| 都江堰市| 彭水| 呼图壁县| 清远市| 琼中| 额尔古纳市| 彭泽县| 得荣县| 永春县| 丰顺县| 宁强县| 华亭县| 汝南县| 固阳县| 九江市| 丹巴县| 塔城市| 凌海市| 招远市|