js修改地址栏但不刷新页面

var stateObject = {id: txt};
	var title = "标题 "+txt;
	var newUrl = txt;
	history.pushState(stateObject,title,newUrl);

  

for(i=0;i<5;i++){
  var stateObject = {id: i};
  var title = "Wow Title "+i;
  var newUrl = "/my/awesome/url/"+i;
  history.pushState(stateObject,title,newUrl);
  alert(i);
}

window.addEventListener('popstate', function(event) {
  readState(event.state);
});

function readState(data){
  alert(data.id);
}

 

posted @ 2017-07-27 16:16  野紫  阅读(6397)  评论(0编辑  收藏  举报