转自:https://www.cnblogs.com/lxl57610/p/7426326.html
window.location.href、location.href是本页面跳转
parent.location.href是上一层页面跳转
top.location.href是最外层的页面跳转
top.location.href=”url”          在顶层页面打开url(跳出框架)  
self.location.href=”url”         仅在本页面打开url地址  
parent.location.href=”url”      在父窗口打开Url地址   
this.location.href=”url”       用法和self的用法一致    
if (top.location == self.location) 判断当前location 是否为顶层来 禁止frame引用   如果页面当中有自定义的frame的话,
也可以将parent  self   top换为自定义frame的名称      效果就是在自定义frame窗口打开url地址

实际中可能这样使用

if(top !== self){             top.location.href = location.href;         }   禁止frame引用

以下是从网上找到的一个例子,不是很直观, 我加了上面那三行代码, 可以先去掉, 再加上, 看一下效果,就很清楚了 以下是top.htm 代码 <script language=javascript> function rs(){

if(top !== self){             top.location.href = location.href;         }   parent.left.location.href="top.htm" parent.bot.location.href="top.htm" } < /script> < input type=button name=name value="ksdj" οnclick=rs();>
以下是一个随意文件名的htm文件: <FRAMESET COLS="150,*"> < FRAME SRC="left.htm" name=left> < FRAMESET ROWS="150,*"> < FRAME SRC="top.htm" name=top> < FRAME SRC="bot.htm" name=bot> < /FRAMESET> < /FRAMESET> 你自己试试,我想你要的可能就是这样的效果!

top表示主窗口,location表示当前窗口,如果你的文件只有一个框架,没有iframe和frmaeset,那么是完全一致的,没有区别。

top.location是在顶层frame中打开新页   window.location是在当前frame中打开新页
parent.location 在当前窗口的父窗口打开Url地址
top表示主窗口,location表示当前窗口,如果你的文件只有一个框架,没有iframe和frmaeset,那么是完全一致的,没有区别。

一:提出问题

使用js的同学一定知道js的location.href的作用是什么,但是在js中关于location.href的用法究竟有哪几种,究竟有哪些区别,估计很多人都不知道了。

blog已经迁移到这里了,有更多文章,欢迎大家访问。

二:常见的几种形式

目前在开发中经常要用到的几种形式有:

1
2
3
4
5
6
self.location.href;
window.location.href;
this.location.href;
location.href;
parent.location.href;
top.location.href;                                          

经常见到的大概有以上几种形式。

三:代码部分

那么,这几种形式的跳转究竟有什么区别呢?

直接讲定义,你肯定不会理解透彻,下面我来贴四个html代码,用实际的例子讲解。

a.html:

1
2
3
4
5
<form id="form1" action="">
<div><strong>这是a.html页面<strong>
<iframe src="b.html" width="500px" height="300px"></iframe> </strong></strong></div>
</form>
<pre>

b.html:

1
2
<span>这是b.html</span><span id="span1"></span><br />
<iframe src="c.html" width="500px" height="300px"></iframe>

c.html:

1
2
<span><strong>这是c.html:<strong></span><span id="span1"></span><br />
<iframe src="d.html" width="500px" height="300px"></iframe>

d.html:

1
2
<span>这是d.html:</span><span id="span1"></span><br />
<input type='button' onclick='jump();' value='跳转'>

a.html,b.html,c.html,d.html通过iframe给联系到了一起,那么它们有什么的联系呢?

观察代码,我们可以看出:

a.html里面嵌着b.html;
b.html里面嵌着c.html;
c.html里面嵌着d.html

四:测试几种用法

下面来测试上述几种写法.

在d.html里面head部分写js:

1
2
3
4
5
6
7
function jump() {
//经测试:window.location.href与location.href,self.location.href,location.href都是本页面跳转
//作用一样
window.location.href="http://www.baidu.com";
//location.href="http://www.baidu.com";
//self.location.href="http://www.baidu.com"; //this.location.href="http://www.baidu.com";
//location.href="http://www.baidu.com"; }

对比图一和图二的变化,你会发现d.html部分已经跳转到了百度的首页,而其它地方没有发生变化。这也就解释了"本页跳转"是什么意思。

好,再来修改d.html里面的js部分为:

1
2
3
4
function jump()
{
    parent.location.href='http://www.baidu.com';
}

运行a.html后,再次点击"跳转" 按钮,运行结果贴图三如下:

对比图一和图三,你会发现a.html中嵌套的c.html部分已经跳转到了百度首页。

分析:我点击的是a.html中嵌套的d.html部分的跳转按钮,结果是a.html中嵌套的c.html部分跳转到了百度首页,这就解释了"parent.location.href是上一层页面跳转"的意思。
再次修改d.html里面的js部分为:

1
2
3
4
function jump()
{
    top.location.href='http://www.baidu.com';
}

运行a.html后,再次点击"跳转" 按钮,

你会发现,a.html已经跳转到了百度首页。

分析:我点击的是a.html中嵌套的d.html部分的跳转按钮,结果是a.html中跳转到了百度首页,这就解释了"top.location.href是最外层的页面跳转"的意思。

转载于:https://www.cnblogs.com/sharpest/p/7722768.html

top.location.href和localtion.href有什么不同相关推荐

  1. top.location.href和localtion.href代码剖析

    $("#updateform").ajaxSubmit(function(data) { alert(data);if(data){self.location.href=" ...

  2. self.location.href、top.location.href、localtion.href、有什么不同

    top.location.href="url"          在顶层页面打开url(跳出框架) self.location.href="url"       ...

  3. js中window.location.href,location.href,parent.location.href,top.location.href的用法

    "window.location.href"."location.href"是本页面跳转 "parent.location.href"是上一 ...

  4. 关于js中window.location.href,location.href,parent.location.href,top.location.href的使用方法

    关于js中"window.location.href"."location.href"."parent.location.href".&qu ...

  5. [网络收集]avascript中top.location.href 与 location.href的区别

    top表示最顶级的窗口,也就是最外层的窗口.如果一个大窗口中嵌套了几个小窗口,那么在小窗口中使用top就表示最外面的大窗口,就是这个意思了. top表示主窗口,location表示当前窗口,如果你的文 ...

  6. top.location.href

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

  7. IE6下top.location.href失效的问题

    IE6下top.location.href失效的问题一般是因为在触发此事件 的按钮上的Onclick事件中没有加return false的原因,加上即可执行. <a href="jav ...

  8. 关于js中window.location.href、location.href、parent.location.href、top.location.href的用法...

    关于js中"window.location.href"."location.href"."parent.location.href".&qu ...

  9. 关于js中window.location.href,location.href,parent.location.href,top.location.href的用法

    "window.location.href"."location.href"是本页面跳转. "parent.location.href" 是 ...

最新文章

  1. logstash配置文件
  2. 在Ubuntu中使用OTB-50测试ECO模型
  3. vant-image本地图片无法显示
  4. C++Fenwick tree芬威克树的实现算法(附完整源码)
  5. java j集合_JNotes/Java-集合篇(2)集合之List.md at master · harryjudy2240/JNotes · GitHub...
  6. silverlight中如何方便在多个场景即Xaml文件之间随意切换?
  7. Hadoop学习之Combiner
  8. python3学习之反射的四种基本方法
  9. 数据可视化工具的特点有哪些
  10. VSCode Debug API
  11. 使用Atmel Studio7和USBasp烧写器烧写AVR单片机
  12. [定理证明]正态随机过程又是马尔科夫过程的充要条件(高斯-马尔科夫过程的充要条件)...
  13. pcl小知识(十二)—— 斯坦福兔子和其他图形学模型数据下载
  14. 数据结构教程(详细又简单——C语言实现)
  15. 回望中国计算机学会CCF十大历史贡献
  16. Windows电脑垃圾的清理
  17. matlab中单刀双掷开关,proteus 怎样找单刀双掷开关
  18. silverlight ajax调用,基于RIA的AJAX和Silverlight研究与应用
  19. 补天:给黑客一个平台 换网络一份安宁!
  20. 软考非计算机专业考难吗,非计算机专业考软考初级哪个更容易过

热门文章

  1. Oracle收购云安全创企Palerra,以加强安全堆栈
  2. 《Angular从零到一》导读
  3. 在Map 3D显示管理器中更改当前地图的名字
  4. 第三天 css核心属性
  5. 交换机入门配置:IP和远程登录功能
  6. Java多线程初学者指南(12):使用Synchronized块同步变量
  7. 動態語句的使用方法(exec/sp_executesql)
  8. 用 Python 读写 Excel 表格,就是这么的简单粗暴且乏味
  9. MATLAB代写要求应该怎么写,matlab/simulink程序代写
  10. SpringCloud(Gateway网关跨域)