Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect and reload the right way in Dart?

Tags:

What are the preferred ways to do a redirection and a reload in Dart?

Do we just use: window.location.href = window.location.href?

like image 562
Tower Avatar asked Oct 28 '12 13:10

Tower


1 Answers

There are a few different ways to handle URI changes and each have their own purpose.

  1. 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.

  2. 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.

Summary

  • I want to simulate a click on <a> tag.

Use window.location.assign(url).

  • I want to redirect to a new website like the HTTP redirection and skip the back-button history.

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.

Not available, maybe in the future. It would probably be window.location.reload(true).

like image 155
Kai Sellgren Avatar answered Sep 20 '22 16:09

Kai Sellgren