在 Dart 中进行重定向和重新加载的首选方法是什么?
我们是否只使用:window.location.href = window.location.href
?
有几种不同的方法可以处理 URI 更改,每种方法都有自己的用途。
-
当您想要将用户发送到另一个 URI 时:
-
window.location.assign('http://google.com')
这会将用户发送到 Google,并保留浏览历史记录(后退按钮历史记录)。这就像点击链接一样。
-
window.location.href = 'http://google.com'
和上面的方法一样,只是另一种方法而已。href
是一个 setter,并导致分配发生。我感觉以前的版本更干净。
-
window.location.replace('http://google.com');
但是,那replace() http://api.dartlang.org/docs/bleeding_edge/dart_html/LocalLocation.html#reload方法上LocalLocation http://api.dartlang.org/docs/bleeding_edge/dart_html/LocalLocation.html对象不仅会将用户发送到 Google,而且不会将原始页面放入会话历史记录中,这意味着用户不会遭受永无休止的后退按钮噩梦。
这本质上与 HTTP 重定向相同。历史被跳过。
-
当您想要重新加载/刷新时。
-
window.location.assign(window.location.href)
将当前页面重新加载到完全相同的 URI。这确实not包含POST
数据。某些资源(如图像等)可能会从缓存中重新加载,因此可能不是完全重新加载。
This is essentially the same as pressing F5 and skipping the sending of POST
data.
-
window.location.href = window.location.href
再次,与之前相同。
-
window.location.reload()
这种重新加载页面的方式也会导致POST
要发送的数据。 “JavaScript 版本”window.location.reload() https://developer.mozilla.org/en-US/docs/DOM/window.location还支持指定是否跳过缓存的参数。但是,当前的 Dart 实现不支持该参数,并且默认从缓存中获取资源。
This cache parameter may be added to Dart at some point, but it's not there yet. When it arrives, you most likely just pass true
as the first parameter and then it would be like Ctrl + Shift + R.
Summary
Use window.location.assign(url)
.
- 我想重定向到一个新网站,例如 HTTP 重定向,并跳过后退按钮历史记录。
Use window.location.replace(url)
.
- I want to do an F5 with
POST
data.
Use window.location.reload()
.
- I want to do an F5 without
POST
data.
Use window.location.assign(window.location.href)
.
- I want to do an Ctrl + Shift + F5.
不可用,也许将来可用。可能会是window.location.reload(true)
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)