What are the preferred ways to do a redirection and a reload in Dart?
Do we just use: window.location.href = window.location.href
?
There are a few different ways to handle URI changes and each have their own purpose.
When you want to send the user to another URI:
window.location.assign('http://google.com')
This one sends the user to Google, keeping the browsing history (the back button history). This is like clicking on a link.
window.location.href = 'http://google.com'
The same as above, just another way to do it. href
is a setter, and causes the assignment to happen. I feel the previous version is cleaner.
window.location.replace('http://google.com');
However, the replace()
method on LocalLocation
object does not only send the user to Google, but also does not put the originating page in the session history, which means the user will not suffer from the never-ending back-button nightmare.
This is essentially the same as an HTTP redirect. The history is skipped.
When you want to do a reload/refresh.
window.location.assign(window.location.href)
Reloads the current page to the exact same URI. This does not contain POST
data. Some of the resources (like images, etc.) may me reloaded from the cache, so it might not be a full reload.
This is essentially the same as pressing F5 and skipping the sending of POST
data.
window.location.href = window.location.href
Again, the same as previous.
window.location.reload()
This way of reloading the page causes also the POST
data to be sent. The "JavaScript version" of window.location.reload()
also supports a parameter that specifies whether to skip the cache or not. However, the current Dart implementation does not support that parameter, and defaults to fetch the resources from cache.
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.
<a>
tag.Use window.location.assign(url)
.
Use window.location.replace(url)
.
POST
data.Use window.location.reload()
.
POST
data.Use window.location.assign(window.location.href)
.
Not available, maybe in the future. It would probably be window.location.reload(true)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With