解决移动端页面window.location.replace不生效的问题

window.location.replace在pc端浏览器中可以正常使用,但是在移动端的h5页面包括app的webview嵌套h5页面之后,window.location.replace不生效,并不能直接替换当前历史记录。

解决办法:
为了解决移动端h5不兼容window.location.replace的问题,我们可以通过window.history.replaceState来代替window.location.replace。

具体解决方案如下:

function locationReplace(url){
    
    
  if(history.replaceState){
    
    
    history.replaceState(null, document.title, url);//window.history.replaceState,其替换的url,必须和当前页面url是同源的,否则不生效
    history.go(0);
  }else{
    
    
    location.replace(url);
  }
}

特别需要注意的是,window.history.replaceState,其替换的url,必须和当前页面url是同源的,否则不生效

原文链接:https://blog.csdn.net/Boale_H/article/details/121720247

猜你喜欢

转载自blog.csdn.net/Quentin0823/article/details/128852698