关于javascript:windowlocationhref与windowopen的区别

9次阅读

共计 1176 个字符,预计需要花费 3 分钟才能阅读完成。

一、比拟罕用的 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 怎么跳转新窗口

正文完
 0