前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >window location href页面跳转的几种用法及其延展「建议收藏」

window location href页面跳转的几种用法及其延展「建议收藏」

作者头像
全栈程序员站长
发布2022-09-14 16:02:53
5.1K0
发布2022-09-14 16:02:53
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

一、location.href常见的几种形式

  1. self.location.href;//当前页面打开URL页面
  2. window.location.href;//当前页面打开URL页面
  3. this.location.href;//当前页面打开URL页面
  4. location.href;// 当前页面打开URL页面
  5. parent.location.href;//在父页面打开新页面
  6. 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提交数据.

用window.open()打开新页面 但是用window.location.href=”” 却是在原窗口打开的. 有时浏览器会一些安全设置window.open肯定被屏蔽。例如避免弹出广告窗口。

window.location是window对象的属性,而window.open是window对象的方法 window.location是你对当前浏览器窗口的URL地址对象的参考! window.open是用来打开一个新窗口的函数!

然而 window,open()不一定是打开新窗口

在有窗口的名称和window.open中第二个参数中的一样就会将这个窗口替换,用这个特性的话可以在iframe和frame中来代替location.href。 如<iframe name=”aa”></iframe> <input type=button οnclick=”window.open(‘1.htm’,’aa’,”)”>和 <input type=button οnclick=”self.frames[‘aa’].location.href=’1.htm'”>的效果一样

二、location.href不同形式之间的区别

a.html:

<form id=”form1″ action=””> <div><b>这是a.html页面<b> <iframe src=”b.html” width=”500px” height=”300px”></iframe> </b></b></div> </form> <pre>

b.html:

<span>这是b.html</span><br /> <iframe src=”c.html” width=”500px” height=”300px”></iframe>

c.html:

<span><b>这是c.html:</b></span><br /> <iframe src=”d.html” width=”500px” height=”300px”></iframe>

d.html:

<span>这是d.html:</span><br /> <input type=’button’ οnclick=’jump();’ value=’跳转’> <iframe src=”d.html” width=”500px” height=”300px”></iframe>

a.html里面嵌着b.html;b.html里面嵌着c.html;c.html里面嵌着d.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”; //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’; }

则c.html 跳转为百度,这就说明了”parent.location.href是上一层页面跳转”即父级页面跳转。 再次修改d.html里面的js部分为:

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

刷新a.html后,再次点击”跳转” 按钮,a.html已经跳转到了百度首页,这就解释了”top.location.href是最外层的页面跳转”即为最上层页面跳转。

三、location.href总结

“top.location.href”是最外层的页面跳转(最上层) “window.location.href”、”location.href”是本页面跳转 “parent.location.href”是上一层页面跳转(父级).

location是window对象的属性,而所有的网页下的对象都是属于window作用域链中(这是顶级作用域),所以使用时是可以省略window。而top是指向顶级窗口对象,parent是指向父级窗口对象。

延展

window.location或window.open如何指定target?

//方法一 window.location 改为 top.location 即可在顶部链接到指定页 //方法二 window.open(“你的网址”,”_top”);

另:

window.open()是可以在一个网站上打开另外的一个网站的地址 window.location()是只能在一个网站中打开本网站的网页

本文参考互联网上多篇博客编辑修改完成,展示出来供大家参考使用

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158184.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、location.href常见的几种形式
  • 二、location.href不同形式之间的区别
  • 三、location.href总结
  • 延展
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档