📜  javascript window.onpopstate 示例 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:48.054000             🧑  作者: Mango

代码示例1
window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
  const urlParams = new URLSearchParams(window.location.search);
  const myParam = urlParams.get('myParam');
  //update model accordingly
};

history.pushState({page: 1}, "title 1", "?page=1");
history.pushState({page: 2}, "title 2", "?page=2");
history.replaceState({page: 3}, "title 3", "?page=3");
history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
history.back(); // alerts "location: http://example.com/example.html, state: null
history.go(2);  // alerts "location: http://example.com/example.html?page=3, state: {"page":3}