改变浏览器URL不跳转 history.pushState

it2023-04-06  78

公司产品较多,涉及到不同站点相互跳转传参,携带的参数使用后不删除便会重复渲染,目前想到的办法就是第一次执行成功后就移除location.search;

使用 history.pushState(state, title [, url]) 方法,向浏览器的会话历史记录堆栈中添加记录。

// 当前页面渲染成功后 将新地址添加到浏览器的会话历史记录堆栈中。 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 // }

文档 https://developer.mozilla.org/en-us/docs/web/api/history/pushstate 流程图 https://www.processon.com/view/link/5ff556b9e0b34d34e1315bde

如果有更改好的建议,欢迎在下方留言。

最新回复(0)