iframe 跳转到其他页面
也是第一次用這個(gè)標(biāo)簽 嵌套其他網(wǎng)站有的跳轉(zhuǎn)了 本以為是自己的問(wèn)題 百度才得知 是其他網(wǎng)站 有如下代碼
if (top.location != self.location) {top.location=self.location;}
會(huì)自動(dòng)判斷當(dāng)前的location是否是頂層的,即是否被嵌套到iframe里面了,如果是,則強(qiáng)制跳轉(zhuǎn)。
解決辦法有兩個(gè)
雙重iframe的確可以阻止強(qiáng)制跳轉(zhuǎn)。但是,第一層的iframe就覆蓋了第二層的。所以要把第一層的做成透明的,然后第二層嵌套該嵌套的網(wǎng)頁(yè)。
security="restricted"是IE的禁止js的功能,會(huì)導(dǎo)致其他js方法跳轉(zhuǎn)不成功
sandbox="" 是H5的功能。可以讓IE,Chrome,Firefox這三大瀏覽器都實(shí)現(xiàn)了禁止iframe的自動(dòng)跳轉(zhuǎn)
<iframesecurity="restricted" sandbox=""src="https://blog.csdn.net/chuan0106"/>而我的做法是 判斷 url 地址是否 === 會(huì)跳轉(zhuǎn)的url 如果是就使用該兩個(gè)方法
當(dāng)前URL地址=== 會(huì)跳轉(zhuǎn)的URL地址 ? <iframe security="restricted" sandbox="" frameBorder="0" src={URL地址} /> : <iframe frameBorder="0" src={URL地址} />防止自己的網(wǎng)站被別人 iframe 嵌套
在head位置中添加該代碼
<head><script language="javascript"> if (top.location != location) { top.location.href = location.href; } </script> </head>或者
<head><script language="javascript"> if(self!=top){top.location.href=self.location.href;} </script></head>這種如果別人嵌套了你的網(wǎng)站 就會(huì)自動(dòng)跳轉(zhuǎn)到你的網(wǎng)站 而非嵌套
但是 當(dāng)別人用類(lèi)似下面代碼做iframe嵌套調(diào)用時(shí),會(huì)躲避你的javascript代碼。
偽代碼:
<iframe src="你的頁(yè)面地址" name="tv" marginwidth="0" marginheight="0" scrolling="No" noResize frameborder="0" id="tv" framespacing="0" width="580" height="550" VSPACE=-145 HSPACE=-385></iframe> <script language="javascript"> var location=""; var navigate=""; frames[0].location.href=""; </script>更好的解決辦法
這里賦值為空頁(yè)面,也可賦值為你的頁(yè)面的URL地址.
js 解決
另外一種屏蔽iframe方法
html 解決
總結(jié)
以上是生活随笔為你收集整理的iframe 跳转到其他页面的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 做工作必须将心比心——感谢译者陈浩对我们
- 下一篇: 将心比心,你对别人好,别人才会对你好,是