location.href的几种用法

location.href

在js中,location.href经常用于页面跳转。它主要有如下几种用法:
1.在当前页面打开URL页面。需要注意的是window.location.href=window.location.hrefwindow.location.reload()都是刷新当前页面,但是如果页面上有提交数据的时候,window.location.reload()会提示是否提交。

self.location.href="url"
location.href="url"
window.location.href="url"
this.location.href="url"

2.在父页面打开URL页面。

parent.location.href="url"

3.在顶层页面打开URL页面。

top.location.href="url"

window.open

window.open方法用于在一个新窗口中打开URL页面。

window.open("url")

上述代码等价于

<a href="url" target="_blank">xxx</a>

也可以设置第二个参数为“_self” 来实现在当前窗口中打开URL页面。

window.open("url","_self")

上述代码等价于

<a href="url" target="_self">xxx</a>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容