blob: f5a991a3ee7eabd741474fd2151d3d79720f5f2c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
---
title: Manipulating browser history
order: 11
layout: page
---
[[advanced.pushstate]]
= Manipulating browser history
A major issue in AJAX applications is that as they run in a single web page.
Bookmarking the application URL (or more generally the __URI__) can only
bookmark the application, not an application state. This is a problem for many
applications, such as product catalogs and discussion forums, in which it would
be good to provide links to specific products or messages. The solution is to
modify the URI of the browser using https://developer.mozilla.org/en-US/docs/Web/API/History_API[History APIs]
[methodname]#pushState# or [methodname]#replaceState# functions, whenever developer
wants to simulate a logical page change. There is a server side API for those
methods and a mechanism to listen to changes in the client side URI in the
[classname]#Page# object.
Vaadin offers two ways to modify URIs: the high-level
[classname]#Navigator# utility described in
<<advanced-navigator#advanced.navigator,"Navigating
in an Application">> and the low-level API described here.
[[advanced.urifu.setting]]
== Setting the URL displayed in the browser
You can set the current fragment identifier with the
[methodname]#pushState()# method in the [classname]#Page# object.
[source, java]
----
Page.getCurrent().pushState("mars");
----
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.
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.
[[advanced.pushstate.popstate]]
== Listening for "in-page" URI Changes
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#.
For example, we could define the listener as follows in the [methodname]#init()#
method of a UI class:
[source, java]
----
public class MyUI extends UI {
@Override
protected void init(VaadinRequest request) {
getPage().addPopStateListener( e -> enter() );
// Read the initial URI fragment
enter();
}
void enter() {
URI location = getPage().getLocation();
... initialize the UI ...
}
}
----
|