博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
location.href用法
阅读量:4132 次
发布时间:2019-05-25

本文共 1925 字,大约阅读时间需要 6 分钟。

location.href用法

javascript中的location.href有很多种用法,主要如下。

self.location.href=”/url” 当前页面打开URL页面location.href=”/url” 当前页面打开URL页面 (相当于省略了前面的window,location是window的属性)window.location.href=”/url” 当前页面打开URL页面,前面三个用法相同。 this.location.href=”/url” 当前页面打开URL页面 parent.location.href=”/url” 在父页面打开新页面 top.location.href=”/url” 在顶层页面打开新页面

self.location.href;window.location.href;this.location.href;location.href;parent.location.href;top.location.href;
如果页面中自定义了frame,那么可将parent self top换为自定义frame的名称,效果是在frame窗口打开url地址 。
此外,window.location.href=window.location.href;和window.location.Reload()和都是刷新当前页面。区别在于是否有提交数据。当有提交数据时,window.location.Reload()会提示是否提交,window.location.href=window.location.href;则是向指定的url提交数据。 

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

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

a.html:

这是a.html页面

b.html:

这是b.html

c.html:

这是c.html:

d.html:

这是d.html:

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:

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"; }

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

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

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部分为:

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

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

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

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

转自:

转载地址:http://cxjvi.baihongyu.com/

你可能感兴趣的文章
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
Mysql复制表以及复制数据库
查看>>
Linux分区方案
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>
Ribbon 学习(三):RestTemplate 请求负载流程解析
查看>>
深入理解HashMap
查看>>
XML生成(一):DOM生成XML
查看>>
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
collect2: ld returned 1 exit status
查看>>
C#入门
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>