1、判断页面是否被iframe有三种方法
//方式一 if (self.frameElement && self.frameElement.tagName == "IFRAME") { alert('在iframe中'); } //方式二 if (window.frames.length != parent.frames.length) { alert('在iframe中'); } //方式三 if (self != top) { alert('在iframe中'); }
2、禁止页面被别人iframe了
<script language="javascript"> if (top.location != location) { top.location.href = location.href; } </script> //或 <script language="javascript"> if(self!=top){top.location.href=self.location.href;} </script>
--注: 这种做法虽简单,但如果对方用如下招数很容易就被破解了
<iframe src="你的页面地址" 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>
当然,万能的js依旧设计了应对招数
<script language="javascript"> if(top != self){ location.href = "about:blank"; //也可设置为你自己的URL } </script>
当然,当然,这个也不是完美的奥,这种方式会禁止所有的页面的嵌入,那么本域名内的页面嵌入也是被禁止呢,嘤嘤~别着急,JS say no~ no~ no~
我们依旧有办法可以做到禁止除域名外的页面的iframe
<script language="JavaScript"> try{ top.location.hostname; if (top.location.hostname != window.location.hostname) { top.location.href =window.location.href; } } catch(e){ top.location.href = window.location.href; } </script>
就是这么完美~