javascript中window.location.href的用法

 更新时间:2023年08月21日 10:03:55   作者:网络真危险!!  
window.location.href 是一个用于获取当前页面 URL 或让浏览器跳转到新 URL 的重要方法,本文就详细的介绍一下javascript中window.location.href的用法,感兴趣的可以了解一下

一、前言

window.location.href 是一个用于获取当前页面 URL 或让浏览器跳转到新 URL 的重要方法,是 window.location 对象的属性。它返回一个字符串,表示当前页面的 URL;同时,当通过将 URL 指定给 window.location.href 时,可以让浏览器跳转到新的 URL。

二、常见用例

  • self.location.href="/url" 当前页面打开URL页面
  • location.href="/url :当前页面打开URL页面
  • window.location.href="/url" :当前页面打开URL页面,前面三个用法相同。
  • this.location.href="/url" :当前页面打开URL页面
  • parent.location.href="/url" :在父页面打开新页面
  • top.location.href="/url" :在顶层页面打开新页面
  • 如果页面中自定义了 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提交数据

  • 如果要关闭当前窗口,并且在新窗口打开某一链接
var a = document.createElement('a')
      a.setAttribute('href', href)
      a.setAttribute('target', '_blank')
      a.setAttribute('id', 'startTelMedicine')
      a.onclick = function () {
        //关闭窗口的方法
        window.opener = null
        window.open('', '_self', '')
        window.close()
      }
      // 防止反复添加
      if (document.getElementById('startTelMedicine')) {
        document.body.removeChild(document.getElementById('startTelMedicine'))
      }
      document.body.appendChild(a)
      a.click()
  • 如果无法关闭当前弹框 说明可能有父节点,可以试试:window.parent.close();

三、window.location.href和window.open的区别

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

2、window.open不一定是打开一个新窗口!!!!!!!!   
只要有窗口的名称和window.open中第二个参数中的一样就会将这个窗口替换,用这个特性的话可以在iframe和frame中来代替location.href。 

<iframe name="aa"></iframe>   
<input type=button   onclick="window.open('1.htm','aa','')">和   
<input type=button   
onclick="self.frames['aa'].location.href='1.htm'">

的效果一样 

3、在给按钮、表格、单元格、下拉列表和DIV等做链接时一般都要用Javascript来完成,和做普通链接一样,可能我们需要让链接页面在当前窗口打开,也可能需要在新窗口打开,这时我们就可以使用下面两项之一来完成:

window.open 用来打开新窗口 
window.location 用来替换当前页,也就是重新定位当前页 
可以用以下来个实例来测试一下。 

<input type="button" value="新窗口打开" onclick="window.open('http://www.google.com')"> 
<input type="button" value="当前页打开" onclick="window.location='http://www.google.com/'"> 

4、window.location或window.open如何指定target?
这是一个经常遇到的问题,特别是在用frame框架的时候
解决办法:

window.location 改为 top.location 即可在顶部链接到指定页 

window.open("你的网址","_top"); 

5、window.open 用来打开新窗口 
window.location 用来替换当前页,也就是重新定位当前页 

用户不能改变document.location(因为这是当前显示文档的位置)。 
window.location本身也是一个对象。 

但是,可以用window.location改变当前文档 (用其它文档取代当前文档),而document.location不是对象。 
服务器重定向后有可能使document.url变动,但window.location.href指的永远是访问该网页时用的URL. 
大多数情况下,document.location和location.href是相同的,但是,当存在服务器重定向时,document.location包含的是已经装载的URL,而location.href包含的则是原始请求的文档的URL.

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

 到此这篇关于javascript中window.location.href的用法   的文章就介绍到这了,更多相关javascript window.location.href内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论