javascript window onpopstate example

Solutions on MaxInterview for javascript window onpopstate example by the best coders in the world

showing results for - "javascript window onpopstate example"
Amy
11 Jan 2017
1window.onpopstate = function(event) {
2  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
3  const urlParams = new URLSearchParams(window.location.search);
4  const myParam = urlParams.get('myParam');
5  //update model accordingly
6};
7
8history.pushState({page: 1}, "title 1", "?page=1");
9history.pushState({page: 2}, "title 2", "?page=2");
10history.replaceState({page: 3}, "title 3", "?page=3");
11history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
12history.back(); // alerts "location: http://example.com/example.html, state: null
13history.go(2);  // alerts "location: http://example.com/example.html?page=3, state: {"page":3}