使用history.replaceState 修改url 不跳转
2024-09-07 09:02:37
history.replaceState(null,null,this.urlR); //关键代码
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);
- }
最新文章
- 对于一个div下两个横内元素对其或者居中的方法
- java接口
- ServletFileUpload(Servlet文件上传)
- Command Network
- python selenuim使用代理的方式
- oracle 内联同时删除多表
- SQLServer temporary table and table variable
- uTenux&mdash;&mdash;重新整理底层驱动库
- D3.js 力导向图的制作
- php with bootstrap
- Fast Matrix Operations
- Python:模块引用
- C++#define的用法(含特殊)
- php订单生成唯一Id
- 数据库事务的4个特性ACID
- 使用exec和sp_executesql动态执行SQL语句(转载)
- Web API中的Help Page
- C#中二进制、十进制和十六进制互相转换的方法
- leetcode刷题笔记258 各位相加
- Redhat6.5安装DB2 Express-C版本
热门文章
- bzoj 3456 城市规划 —— 分治FFT / 多项式求逆 / 指数型生成函数(多项式求ln)
- 整理出来的一个windows关机、锁定、重启、注销 API调用
- caffe solver
- git pull 冲突
- 一步步实现 Prism + MEF(二)--- 绑定命令
- 《精通Spring4.X企业应用开发实战》读后感第五章(注入参数详解)
- prototype for &#39;类名::函数名&#39;does not match any in class&#39;类名&#39;
- JAVA之BigInteger(转)【转】【很好用啊】
- Linux绘图函数
- 萌新笔记之Nim取石子游戏