history.replaceState(null,null,this.urlR);  //关键代码

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

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

示例:

先保存三个页面

goto1.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>goto1</title>
  6. </head>
  7. <body>
  8. <h1>这是goto1</h1>
  9. <a href='goto2.html'>去2</a>
  10. </body>
  11. </html>

goto2.html

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



goto3.html

  1. <pre name="code" class="html"><!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>goto3</title>
  6. </head>
  7. <body>
  8. <h1>这是goto3</h1>
  9. </body>
  10. </html>




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

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

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

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

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

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

最新文章

  1. 对于一个div下两个横内元素对其或者居中的方法
  2. java接口
  3. ServletFileUpload(Servlet文件上传)
  4. Command Network
  5. python selenuim使用代理的方式
  6. oracle 内联同时删除多表
  7. SQLServer temporary table and table variable
  8. uTenux&mdash;&mdash;重新整理底层驱动库
  9. D3.js 力导向图的制作
  10. php with bootstrap
  11. Fast Matrix Operations
  12. Python:模块引用
  13. C++#define的用法(含特殊)
  14. php订单生成唯一Id
  15. 数据库事务的4个特性ACID
  16. 使用exec和sp_executesql动态执行SQL语句(转载)
  17. Web API中的Help Page
  18. C#中二进制、十进制和十六进制互相转换的方法
  19. leetcode刷题笔记258 各位相加
  20. Redhat6.5安装DB2 Express-C版本

热门文章

  1. bzoj 3456 城市规划 —— 分治FFT / 多项式求逆 / 指数型生成函数(多项式求ln)
  2. 整理出来的一个windows关机、锁定、重启、注销 API调用
  3. caffe solver
  4. git pull 冲突
  5. 一步步实现 Prism + MEF(二)--- 绑定命令
  6. 《精通Spring4.X企业应用开发实战》读后感第五章(注入参数详解)
  7. prototype for &#39;类名::函数名&#39;does not match any in class&#39;类名&#39;
  8. JAVA之BigInteger(转)【转】【很好用啊】
  9. Linux绘图函数
  10. 萌新笔记之Nim取石子游戏