js关闭被打开的iframe页面,并在新窗口打开
作者:互联网
示例代码:
$(function(){ console.log(window.location.href) if (self.frameElement && self.frameElement.tagName == "IFRAME") { // alert('在iframe中'); window.open(window.location.href) parent.location.reload(true); var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index); } });
相关知识补充: 判断页面是否被iframe有三种方法
//方式一 if (self.frameElement && self.frameElement.tagName == "IFRAME") { alert('在iframe中'); } //方式二 if (window.frames.length != parent.frames.length) { alert('在iframe中'); } //方式三 if (self != top) { alert('在iframe中'); }
禁止页面被别人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>
但这种方式会禁止所有的页面的嵌入,那么本域名内的页面嵌入也是被禁止 下面做到禁止除域名外的页面的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>
标签:top,js,window,href,location,iframe,打开,self,新窗口 来源: https://www.cnblogs.com/chenyingying0/p/16116419.html