You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-pushstate.asciidoc 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ---
  2. title: Manipulating browser history
  3. order: 11
  4. layout: page
  5. ---
  6. [[advanced.pushstate]]
  7. = Manipulating browser history
  8. A major issue in AJAX applications is that as they run in a single web page.
  9. Bookmarking the application URL (or more generally the __URI__) can only
  10. bookmark the application, not an application state. This is a problem for many
  11. applications, such as product catalogs and discussion forums, in which it would
  12. be good to provide links to specific products or messages. The solution is to
  13. modify the URI of the browser using https://developer.mozilla.org/en-US/docs/Web/API/History_API[History APIs]
  14. [methodname]#pushState# or [methodname]#replaceState# functions, whenever developer
  15. wants to simulate a logical page change. There is a server side API for those
  16. methods and a mechanism to listen to changes in the client side URI in the
  17. [classname]#Page# object.
  18. Vaadin offers two ways to modify URIs: the high-level
  19. [classname]#Navigator# utility described in
  20. <<advanced-navigator#advanced.navigator,"Navigating
  21. in an Application">> and the low-level API described here.
  22. [[advanced.urifu.setting]]
  23. == Setting the URL displayed in the browser
  24. You can set the current fragment identifier with the
  25. [methodname]#pushState()# method in the [classname]#Page# object.
  26. [source, java]
  27. ----
  28. Page.getCurrent().pushState("mars");
  29. ----
  30. The parameter (both String and URI are supported) is resolved on the current URL. Both relative and absolute URIs are supported, but note that browsers accept only URLs of the same origin as the current URL.
  31. A call to _pushState_ creates a new entry to browsers history. If you wish to avoid this, and just replace the current URL in the browser, use the related [methodname]#replaceState# method.
  32. [[advanced.pushstate.popstate]]
  33. == Listening for "in-page" URI Changes
  34. If your application uses pushState to update the location and after that the user uses browsers back/forward button, a full page reload does not happen and the UIs init method is not called like when entering the page for the first time. To detect these change you can use [interfacename]#PopChangeListener#.
  35. For example, we could define the listener as follows in the [methodname]#init()#
  36. method of a UI class:
  37. [source, java]
  38. ----
  39. public class MyUI extends UI {
  40. @Override
  41. protected void init(VaadinRequest request) {
  42. getPage().addPopStateListener( e -> enter() );
  43. // Read the initial URI fragment
  44. enter();
  45. }
  46. void enter() {
  47. URI location = getPage().getLocation();
  48. ... initialize the UI ...
  49. }
  50. }
  51. ----