详解location.href几种用法的区别【JS跳转】

 一:提出问题

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

二:常见的几种形式

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

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


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

三:代码部分

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

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

a.html:

1<form id="form1" action="">
2<div><strong>这是a.html页面<strong>
3<iframe src="b.html" width="500px" height="300px"></iframe> </strong></strong></div>
4</form>
5<pre>
b.html:
1<span>这是b.html</span><span id="span1"></span><br />
2<iframe src="c.html" width="500px" height="300px"></iframe>
c.html:
1<span><strong>这是c.html:<strong></span><span id="span1"></span><br />
2<iframe src="d.html" width="500px" height="300px"></iframe>
d.html:
1<span>这是d.html:</span><span id="span1"></span><br />
2<input type='button' onclick='jump();' value='跳转'>
3<iframe src="d.html" width="500px" height="300px"></iframe>
a.html,b.html,c.html,d.html通过iframe给联系到了一起,那么它们有什么的联系呢?

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

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

运行a.html,贴图一如下:


四:测试几种用法

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

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

01<strong>function jump()
02{
03//经测试:window.location.href与location.href,self.location.href,location.href都是本页面跳转
04//作用一样
05window.location.href="http://www.baidu.com";
06//location.href="http://www.baidu.com";
07//self.location.href="http://www.baidu.com";
08//this.location.href="http://www.baidu.com";
09//location.href="http://www.baidu.com";
10} </strong>
再次运行a.html,点击那个"跳转" 按钮,运行结果贴图二如下:


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

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

1function jump()
2{
3parent.location.href='http://www.baidu.com';
4}
运行a.html后,再次点击"跳转" 按钮,运行结果贴图三如下:


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

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

1function jump()
2{
3top.location.href='http://www.baidu.com';
4}
运行a.html后,再次点击"跳转" 按钮,

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

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

五:总结

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


 一:提出问题

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

二:常见的几种形式

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

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


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

三:代码部分

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

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

a.html:

1<form id="form1" action="">
2<div><strong>这是a.html页面<strong>
3<iframe src="b.html" width="500px" height="300px"></iframe> </strong></strong></div>
4</form>
5<pre>
b.html:
1<span>这是b.html</span><span id="span1"></span><br />
2<iframe src="c.html" width="500px" height="300px"></iframe>
c.html:
1<span><strong>这是c.html:<strong></span><span id="span1"></span><br />
2<iframe src="d.html" width="500px" height="300px"></iframe>
d.html:
1<span>这是d.html:</span><span id="span1"></span><br />
2<input type='button' onclick='jump();' value='跳转'>
3<iframe src="d.html" width="500px" height="300px"></iframe>
a.html,b.html,c.html,d.html通过iframe给联系到了一起,那么它们有什么的联系呢?

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

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

运行a.html,贴图一如下:


四:测试几种用法

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

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

01<strong>function jump()
02{
03//经测试:window.location.href与location.href,self.location.href,location.href都是本页面跳转
04//作用一样
05window.location.href="http://www.baidu.com";
06//location.href="http://www.baidu.com";
07//self.location.href="http://www.baidu.com";
08//this.location.href="http://www.baidu.com";
09//location.href="http://www.baidu.com";
10} </strong>
再次运行a.html,点击那个"跳转" 按钮,运行结果贴图二如下:


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

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

1function jump()
2{
3parent.location.href='http://www.baidu.com';
4}
运行a.html后,再次点击"跳转" 按钮,运行结果贴图三如下:


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

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

1function jump()
2{
3top.location.href='http://www.baidu.com';
4}
运行a.html后,再次点击"跳转" 按钮,

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

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

五:总结

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

首先贴出来网上找的demo,在和小明做的如果·微营销的过程中,要修改一个后台模板,
    <frameset border=0 framespacing=0 rows="60, *" frameborder=0>
        <frame name=head src="./head.html" frameborder=0 noresize scrolling=no>
            <frameset cols="170, *">
                <frame name=left src="./left.html" frameborder=0 noresize />
                <frame name=right src="./right.html" frameborder=0 noresize scrolling=yes />
            </frameset>
    </frameset>
    <noframes>
    </noframes>
后台代码是这个样子,然后在left.html里面的一个a链接跳转到另一个php页面,结果直接在frame里面跳转,其他的frame没有变化,因此百度了一下,发现默认的当前框架跳转,因此
<a href="javascript:toAdd();">添加商品</a>
function toAdd(){
<span style="white-space:pre">	top.location.href= "add.html";</span>
}
就得到了想要的效果。


  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值