常见的几种开发形式:

self.location.href;
window.location.href;
this.location.href;
location.href;
parent.location.href;
top.location.href;

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

通过实际的例子讲解以上的几种形式有什么具体的区别:

总共是4个具体的HTML页面:

a.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>这是a.html页面</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<form id="form1" action="">
<div><strong>这是a.html页面<strong>
<iframe src="b.html" width="500px" height="300px"></iframe> </strong></strong></div>
</form>
</body>
</html>

b.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>这是b.html</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<span>这是b.html</span><span id="span1"></span><br />
<iframe src="c.html" width="500px" height="300px"></iframe>
</body>
</html>

c.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>这是c.html</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<span><strong>这是c.html:<strong></span><span id="span1"></span><br />
<iframe src="d.html" width="500px" height="300px"></iframe>
</body>
</html>

d.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>这是d.html</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<script type="text/javascript">
function jump(){
*********
}
</script>
</head>
<body>
<span>这是d.html:</span><span id="span1"></span><br />
<input type='button' onclick='jump();' value='跳转'>
<iframe src="d.html" width="500px" height="300px"></iframe>
</body>
</html>

打开a.html对应的截图如下:

下面再d.html中写入对应的js代码,看各种跳转是什么样的效果:

    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";
//his.location.href="http://www.baidu.com";
//location.href="http://www.baidu.com";
}

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

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

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

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

显示效果如下:

你会发现a.html中嵌套的c.html部分已经跳转到了百度首页。

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

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

显示效果如下:

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

总结:

看完上面的讲解之后,在来看看下面的定义你就会非常明白了:
"top.location.href"是最外层的页面跳转
"window.location.href"、"location.href"是本页面跳转
"parent.location.href"是上一层页面跳转.

关于location.href几种用法的区别的更多相关文章

  1. Html A标签中 href 和 onclick用法、区别、优先级别

    原文:Html A标签中 href 和 onclick用法.区别.优先级别 如果不设置 href属性在IE6下面会不响应hover.双击后会选中标签的父容器而非这个一a标签(IE下都存在这一问题). ...

  2. location和location.href跳转url的区别

    使用 location = url  跳转,如果本地之前已经载入过该页面并有缓存,那么会直接读取本地的缓存,缓存机制是由本地浏览器设置决定的.状态码为:  200 OK (from cache) . ...

  3. window.location.href 与 window.loaction.replace区别

    window.location.href和window.location.replace的区别 1.window.location.href=“url”:改变url地址: 2.window.locat ...

  4. window.location.href和document.location.href、document.URL的区别

    1.document表示的是一个文档对象,window表示的是一个窗口对象,一个窗口下可以有多个文档对象. 所以一个窗口下只有一个window.location.href,但是可能有多个documen ...

  5. location.replace()和location.href=进行跳转的区别

    location.href 通常被用来跳转到指定页面地址;location.replace 方法则可以实现用新的文档替换当前文档;location.replace 方法不会在 history 对象中生 ...

  6. 【转】Java中super和this的几种用法与区别

    1. 子类的构造函数如果要引用super的话,必须把super放在函数的首位.   class Base {   Base() {   System.out.println("Base&qu ...

  7. A标签中 href 和 onclick用法、区别、优先级别

    (内容摘自:https://blog.csdn.net/chenchunlin526/article/details/77346049) Html A标签中 href 和 onclick 同时使用的问 ...

  8. window.location.href和window.open的几种用法和区别

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

  9. js实现网页防止被iframe框架嵌套及几种location.href的区别

    首先我们了解一下几种location.href的区别简单的说:几种location.href的区别js实现网页被iframe框架功能,感兴趣的朋友可以了解下 首先我们了解一下:window.locat ...

随机推荐

  1. linux下开发c++第二弹--helloworld与makefile

    一:hello world sudo vim hello.cpp 输入  g++ -o hello hello.cpp  获得hello 输入  ./hello 运行,显示结果hello world! ...

  2. work6

    1) 把程序编译通过, 跑起来. 把正确的 playPrev(GoMove) 的方法给实现了. 如果大家不会下围棋,那就需要大家实地或者上网练习一下围棋的死活,提子是怎么回事.这个应该一个小时就能搞定 ...

  3. Linux数据流重定向

    一.什么是数据流重导向: 数据流重导向 (redirect) 由字面上的意思来看,好像就是将『数据传导到其他地方去』,没错-数据流重导向就是将某个命令运行后应该要出现在屏幕上的数据, 给他传输到其他的 ...

  4. Java邮件服务学习之四:邮箱服务客户端Spring Mail

    一.Spring Mail API Spring邮件抽象层的主要包为org.springframework.mail,Spring提供的邮件发送不仅支持简单邮件的发送.添加附件. 1.邮件发送的核心接 ...

  5. JSF 2.0 hello world example

    In this tutorial, we will show you how to develop a JavaServer Faces (JSF) 2.0 hello world example, ...

  6. UINavigationController切换controller动画设置

    http://blog.csdn.net/dabiaoyanjun/article/details/7774775 uinavigationcontrolleranimation在pushViewCo ...

  7. tomcat启动很慢的原因

    启动后tomcat显示的代码如下: 2014-4-3 10:50:15 org.apache.catalina.core.AprLifecycleListener init 信息: The APR b ...

  8. delphi TPopupMenu.Popup

      procedure TPopupMenu.Popup(X, Y: Integer);     这个点是相对桌面的而不是窗体的   GetCursorPos是鼠标的位置 鼠标动这个点就不一样   v ...

  9. Android腾讯微博开发之随机字符串与签名实现

    Android腾讯微博开发入门之随机字符串与签名实现   直接上代码   1.Utils类,包括签名和随机字符串   import java.util.Random; import javax.cry ...

  10. Android 官方文档:(二)应用清单 —— 2.10 &lt;instrumentation&gt;标签

    syntax: <instrumentation android:functionalTest=["true" | "false"]           ...