使用history.replaceState 控制微信返回


history.replaceState是将指定的URL替换当前的URL

注意:用于替换掉的URL必须是同域的

示例:

先保存三个页面

goto1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>goto1</title>
</head>
<body>
    <h1>这是goto1</h1>
    <a href='goto2.html'>去2</a>
</body>
</html>

goto2.html

<!DOCTYPE html>
<html lang="en">
<head>    
<meta charset="UTF-8">    
<title>goto2</title>
</head>
<body>    
<h1>这是goto2</h1>    
<a href='goto3.html'>去3</a>
</body>
<script>
history.replaceState({}, "goto1", "goto1.html");//将该页面的url替换为goto1.html,而不刷新页面
</script>
</body>
</html>


 
 
goto3.html 

<pre name="code" class="html"><!DOCTYPE html>
<html lang="en">
<head>    
<meta charset="UTF-8">    
<title>goto3</title>
</head>
<body>    
<h1>这是goto3</h1>    
</body>
</html>
 
 
 
 

先从goto1点击链接进入goto2,goto2点链接进入goto3;

此时点击浏览器的后退键应该返回到goto2,然而由于我们已经用

history.replaceState({}, "goto1", "goto1.html");将goto2的url历史记录换成goto1;

所有从goto3点击后退直接返回到了goto1页面;这里的goto1也可以换成所有你想要用户返回的页面

奉上个人封装的一个控制返回小函数

var url='goto1';
var param=new Object();
param.userid='123';
param.status='1';//最后得到path=goto1.html?userid=123&status=1 ;
function changeBackUrl(url,param){	//url表示链接地址
 if(typeof(param)=='object'){ 
param=JSON.stringify(param).replace(/\{|\}|\"|\'/g,'').replace(',','&').replace(/:/g,'='); 
}else{ 
try{ param=param.toString().replace(',','&').replace(/:/g,'=').replace(/\"|\'/g,''); }catch(e){''} 
} 
var path=+url+".html?"+param; history.replaceState(null, null, path); 
}


 
 

                
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值