如果你想获取站点的URL信息,那么window.location
对象什么很适合你!使用其属性获取有关当前页面地址的信息,或使用其方法进行某些页面重定向或刷新????
https://segmentfault.com/search?q=前端小智#2
window.location.origin → '"https://segmentfault.com'
.protocol → 'https:'
.host → 'segmentfault.com'
.hostname → 'segmentfault.com'
.port → ''
.pathname → '/search'
.search → '?q=前端小智'
.hash → '#2'
.href → 'https://segmentfault.com/search?q=前端小智#2'
window.location.assign('url')
.replace('url')
.reload()
.toString()
window.location 属性
window.location返回值.origin站点主地址(协议 + 主机名 + 端口).protocol协议架构 (http:
或者 htts:
).host域名 + 端口.port端口.pathname最前页的 '/' 后面跟的路径.search? 后跟的查询字符串.hash从 #
号开始的部分.href完整网址
host 和 hostname 的区别
在上面的示例中,你可能注意到host
和hostname
返回相同的值。那么为什么要这些属性。好吧,这与端口号有关,让我们来看看。
https://segmentfault.com/search
window.location.host; // 'segmentfault.com'
window.location.hostname; // 'segmentfault.com'
window.location.port; // ''
带端口的 URL
https://segmentfault.com/search"8080
window.location.host; // 'segmentfault.com:8080'
window.location.hostname; // 'segmentfault.com'
window.location.port; // '8080'
因此,host
将包括端口号,而hostname
将仅返回主机名。
我们不仅可以调用location` 属性来检索URL信息,还可以使用它来设置新属性并更改URL。
// 开始 'https://segmentfault.com/'
window.location.pathname = '/tidbits'; // 设置 pathname
// 结果 'https://segmentfault.com/tidbits'
下面是你可以更改的属性的完整列表
// 事例
window.location.protocol = 'https'
.host = 'localhost:8080'
.hostname = 'localhost'
.port = '8080'
.pathname = 'path'
.search = 'query string' // (这里不用写 `?`)
.hash = 'hash' // (这里不用写 `#`)
.href = 'url'
唯一不能设置的属性是window.location.origin
,此属性是只读的。
window.location
返回一个Location
对象。它为我们提供有关页面当前地址的信息。但是我们还可以通过几种方式访问Location
对象。
window.location → Location
window.document.location → Location
document.location → Location
location → Location
我们这样做的原因是这些是我们浏览器中的全局变量。
上面四个属性都指向同一个Location
对象。我个人更喜欢window.location
并且实际上会避免使用location
。主要是因为location
看起来像一个普通变量,并且我们有时可能会不小心将其命名为变量,这将覆盖全局变量。举个例子:
// https://www.samanthaming.com
location.protocol; // 'https'
function localFile() {
const location = '/sam';
return location.protocol;
// ❌ undefined
// b/c local "location" has override the global variable
}
我想大多数开发人员都知道window
是一个全局变量。这样就不太可能引起混淆。老实说,直到我写了这篇文章,我才知道location
是一个全局变量。建议大家多使用 window.location
来代替其它写法。
根据 MDN :
此方法返回 URL 的 USVString,它是 Location.href 的只读版本。
换句话说,我们可以这样得到 href
的值:
// https://www.samanthaming.com
window.location.href; // https://www.samanthaming.com
window.location.toString(); // https://www.samanthaming.com
assign vs replace
这两种方法都是重定向或导航到另一个URL。区别在于assign
是将当前页面保存在历史记录中,因此用户可以使用“后退”按钮导航到该页面。而使用replace
方法时,不会保存它。让我们来看一个例子。
1. 打开一个新的空白页
2. 输入 www.samanthaming.com (当前页)
3. 使用 `window.location.assign('https://www.w3schools.com')` 载入新页面
4. 按 "返回上一页"
5. 返回到了 ???? www.samanthaming.com
Replace
1. 打开一个新的空白页
2. 输入 www.samanthaming.com (当前页)
3. 使用 `window.location.assign('https://www.w3schools.com')` 载入新页面
4. 按 "返回上一页"
5. 返回到一个空白页
如何让页面重定向
如何重定向到另一个页面,有3种方法。
window.location.href = 'https://www.samanthaming.com';
window.location.assign('https://www.samanthaming.com');
window.location.replace('https://www.samanthaming.com');
replace vs assign vs href
这三个都可以重定向,区别在于浏览器的历史记录。href
和assign
会把当前页面保存在历史记录中,而replace
则不会。因此,如果你想创建一种导航无法回到原始页面的体验,请使用replace
????
现在的问题是href
与assign
。我更喜欢assign
,因为它是一种方法,因此感觉好像我正在执行一些操作。还有一个额外的好处是它更易于测试。我已经编写了许多Jest
测试,因此通过使用一种方法,它使其更易于模拟。
window.location.assign = jest.fn();
myUrlUpdateFunction();
expect(window.location.assign).toBeCalledWith('http://my.url');
最终希望备忘单,希望能对你有所帮助,在需要的时候,能快速给你带来答案。谢谢大家的观看。
作者:Samantha Ming 译者:前端小智 来源:medium 原文:https://morioh.com/p/b444d291bdfb
相关热门推荐
很多人不知道可以使用这种 key 的方式来对 Vue 组件进行重新渲染!
前端如何提高用户体验:增强可点击区域的大小
周末学会了 10个超级实用 Javascript 技巧!
CSS Viewport 单位,很多人还不知道使用它来快速布局!
30 个纯 HTML5 实现的游戏
很多人不知道 v-for 可以这样解构
小智最近在学习正则,学习过程中发现这 6 个方便的正则表达式
详解 ES10 中 Object.fromEntries() 的缘起
有哪些被低估未被广泛使用的有用的 HTML标签?
【干货】工业软件为什么这么难?