Change the browser URL without redirecting history.pushState

There are many products of the company, which involve the transfer of parameters between different sites, and the parameters carried will be rendered repeatedly if they are not deleted after use. The current method is to remove location.search after the first execution is successful;

Use history.pushState(state, title [, url])method to add records to the session history stack browser.

// 当前页面渲染成功后 将新地址添加到浏览器的会话历史记录堆栈中。
if(location.search){
    
    
    history.pushState('','',location.href.split('?')[0]); // change url and don't jump
}
  
// 智慧工程项目中使用的是未用框架的单页面,用此方法跳转不同菜单
//if(location.search){
    
    
    //history.pushState('','',location.href.split(location.search)[0]+'?xpage='+currentmenu); // change url and don't jump
 // }else{
    
    
    //history.pushState('','',location.href+'?xpage='+currentmenu); // change url and don't jump
 // }

Documentation https://developer.mozilla.org/en-us/docs/web/api/history/pushstate
flow chart https://www.processon.com/view/link/5ff556b9e0b34d34e1315bde

If you have suggestions for changes, please leave a message below.

Guess you like

Origin blog.csdn.net/weixin_42549581/article/details/109181176