点击刷新时修改路径 html,window.location更改URL属性、重定向或刷新页面!!

本篇文章给大家分享一份window.location备忘单!了解如何更改URL属性以及如何在JavaScript中重定向或刷新页面。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

dc932fd955f05ae752365a19b2655a4f.png

如果你想获取站点的URL信息,那么window.location对象什么很适合你!使用其属性获取有关当前页面地址的信息,或使用其方法进行某些页面重定向或刷新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返回相同的值。那么为什么要这些属性。好吧,这与端口号有关,让我们来看看。

没有端口的 URLhttps://segmentfault.com/searchwindow.location.host; // 'segmentfault.com'

window.location.hostname; // 'segmentfault.com'

window.location.port; // ''

带端口的 URLhttps://segmentfault.com/search"8080window.location.host; // 'segmentfault.com:8080'

window.location.hostname; // 'segmentfault.com'

window.location.port; // '8080'

因此,host将包括端口号,而hostname将仅返回主机名。

如何更改 URL 属性

我们不仅可以调用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,此属性是只读的。

Location 对象

window.location返回一个Location对象。它为我们提供有关页面当前地址的信息。但是我们还可以通过几种方式访问Location对象。window.location → Location

window.document.location → Location

document.location → Location

location → Location

我们这样做的原因是这些是我们浏览器中的全局变量。

80a2ba874e3ece643fa0698a231d5351.png

window.location vs 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 来代替其它写法。

window.location 方法方法作用.assign()加载一个新的文档

.replace()用新的文档替换当前文档

.reload()重新加载当前页面

.reload()返回的URL

window.location.toString

根据 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方法时,不会保存它。让我们来看一个例子。

Assign1. 打开一个新的空白页

2. 输入 www.samanthaming.com (当前页)

3. 使用 `window.location.assign('https://www.w3schools.com')` 载入新页面

4. 按 "返回上一页"

5. 返回到了 ???? www.samanthaming.com

Replace1. 打开一个新的空白页

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');

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值