关于javascript:windowlocationhref与windowopen的区别

一、比拟罕用的JS跳转页面和关上新窗口的办法

1、替换当前页 (从新定位当前页)

window.location.href = "https://www.xxx.com"; // 跳转到新的域名
window.location.href = `/dashboard#/setting?type=1&userId=123`; // 在以后域名下跳转到新的子页面

window.location.href = "https://www.xxx.com"; // 跳转到新的域名
window.location.href = `/dashboard#/setting?type=1&userId=123`; // 在以后域名下跳转到新的子页面

2、关上新窗口

window.open("https://www.xxx.com"); // 跳转到新的域名
window.history.back(-1); // 返回到上一页(在以后窗口 )

二、window.location.hrefwindow.open() 的区别

  • 区别一

window.locationwindow对象的属性
window.open()window对象的办法

  • 区别二

window.location.href是用新的域名替换当前页, 也就是从新定位当前页
window.open()是用来关上一个新窗口的函数!

  • 区别三

window.open()可能会被浏览器拦挡
window.location.href不会被窗口拦挡

window.location.hrefdocument.location.href的区别:
window.location.hrefdocument.location.href都能够对以后窗口进行重定向。
(只管 Document.location 是一个只读的 Location 对象,然而也可能赋给它一个 DOMString
当服务器未产生重定向时, 两者是雷同的。
然而当服务器产生了重定向,就不一样了:

  • document.location蕴含的是曾经装载的URL
  • window.location.href蕴含的则是原始申请的文档的URL

三、window.location.href怎么跳转新窗口

window.location.href是在以后窗口进行笼罩,那怎么跳转到新窗口呢?

let tempwindow = window.open('_blank');
tempwindow.location = 'https://www.xxx.com'; // 能够关上新的地址
// tempwindow.location = '/dashboard#/setting?type=1&userId=123'; // 也能够关上原有地址的子页面

参考资料:
window.location.href和window.open的几种用法和区别
window.location.href怎么跳转新窗口

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理