首先我们了解一下几种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地址。如果是则没被嵌套,如果否的话即被嵌套了,这是我们可以提示一下。预览效果

网页防止被框架方法代码:

复制代码代码如下:
<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> 

js实现网页防止被iframe框架嵌套及几种location.href的区别的更多相关文章

  1. js 跨域 Jquery取得iframe中元素的几种方法

    http://www.jb51.net/article/34942.htm 收集利用Jquery取得iframe中元素的几种方法 : 父页面访问子页面 $(document.getElementByI ...

  2. JS 中document.URL 和 window.location.href 的区别

    实际上,document 和 window 这两个对象的区别已经包含了这个问题的答案. document 表示的是一个文档对象,window 表示一个窗口对象. 一个窗口下面可以有很多的documen ...

  3. iframe框架嵌套技巧(全屏,去双滚动条)

    一般情况下我们很少用到iframe(框架),但有些特殊的情况下我们不得不使用iframe,那么或许或遇到嵌套内容不全屏,网页周围有边框,双滚动条等等情况,下面来说一下处理技巧. 全屏与边框处理: &l ...

  4. JS操作网页中的iframe

    /* *parent.html */ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  5. JS中 window.location 与window.location.href的区别

    疑惑:window.location='url'  与window.lcoation.href='url'效果一样,都会跳转到新页面,区别在哪?查得的资料如下: 1:window.location是页 ...

  6. JS 中document.URL 和 windows.location.href 的区别

    实际上,document 和 windows 这两个对象的区别已经包含了这个问题的答案. document 表示的是一个文档对象,windows 表示一个窗口对象.  一个窗口下面可以有很多的docu ...

  7. js如何判断是否在iframe中及防止网页被别站用 iframe嵌套 (Load denied by X-Frame-Options)

    1. js如何判断是否在iframe中 //方式一 if (self.frameElement && self.frameElement.tagName == "IFRAME ...

  8. js 控制框架页面跳转 top.location.herf = "url"

    top.location.href和localtion.href有什么不同 top.location.href=”url”          在顶层页面打开url(跳出框架) self.locatio ...

  9. js中top.location.href、parent.location.href用法

    window.location.href.location.href是本页面跳转 parent.location.href是上一层页面跳转 top.location.href是最外层的页面跳转 举例说 ...

随机推荐

  1. IOS之KVC和KVO(未完待续)

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  2. IOS的UI基础02

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  3. Javascript中的五种数据类型

    Undefined 未定义.只有一个值undefined Null         只有一个值,null Boolean 在javascript中,只要逻辑表达式不返回undefined不返回null ...

  4. 从1.5k到18k, 一个程序员的5年成长之路

    昨天收到了心仪企业的口头offer, 回首当初什么都不会开始学编程, 到现在恰好五年. 整天在社区晃悠, 看了不少的总结, 在这个时间点, 我也写一份自己的总结吧. 我一直在社区分享, 所以, 这篇总 ...

  5. 【SVN】Unable to connect to a repository at URL 'svn://localhost/Test'

    早上配置SVN,但是这次不是那么顺利... 环境: Windows 7 SVN服务器端:CollabNetSubversion-server-1.8.13-1 SVN客户端:TortoiseSVN_V ...

  6. Asp.net MVC 4新项目中创建area的后续操作

    Asp.net MVC 4新项目中创建area后,往往HomeController与area的HomeController路由发生混淆,需要手工设置一些地方避免mvc无法识别默认路由的状况. 无废话具 ...

  7. Python基本数据结构-字典-创建/访问/基本操作/格式化输出

  8. Oracle VirtualBox 使用桥接网络完成主机和虚拟机之间的双向通讯

    最近刚换了新的笔记本电脑,终于使用上intel i7处理器,可以使用硬件虚拟化技术安装系统.配置如下: 主机      ThinkPad P50s   OS Window 10 虚拟机软件  Orac ...

  9. hdu-4811 Ball

    题目链接: Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. Redis安全访问

    限制IP(只能一个) # If you want you can bind a single interface, if the bind option is not # specified all ...