JS 页面发生跳转但是url 不发生变化

有时候总想不改变url进行页面的跳转,而且有时候还要携带一些参数,或者参数还要安全或者过大,使用get或者post请求.

iframe实现:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>default</title>
</head>
<body>
    <iframe src="./index.html" frameborder="0"></iframe>
</body>
</html>

a标签:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>main file</title>
</head>
<body>
    <a href="./a.html">a.html</a>
    <a href="./b.html">b.html</a>
</body>
<script>


</script>
</html>
<!-- a.html  -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>a file</title>
</head>
<body>
    <div>i am a file!</div>
</body>
</html>

window.location.href实现:

window.location.href = "/?" + ({"path": "user/main"});

window.location.href = "/?path=user/main";

ajax实现:

ajax发个get请求,把响应用document.write输出就可以了。
// 路径 参数 返回函数
$.get("/", {"path": "user/main"}, function (res) {
		document.write(res);
});

$.post("/", {"path": "user/main"}, function (res) {
		document.write(res);
});

$.ajax({
	url: "/user/getUser", // 请求url
	type: "POST", // 请求方式
	data: JSON.stringify({"path": "user/main"}), // 请求参数格式化
	// dataType: "json", // 返回数据类型
	async: false, // 开启同步,关闭异步
	contentType: "application/json",
	success: function (res) { // res 响应结果
		// console.log("成功:" + JSON.stringify(res));
		// 保存数据到cookie缓存中
		// localStorage.setItem("menuList", JSON.stringify(menuData.menuList));
		// localStorage.setItem("menuArray", JSON.stringify(menuData.mArray));
		// window.location.href = "/?path=user/main";
		// 将请求返回的页面信息写出,展示在浏览器中(完成url不变,成功跳转页面的请求)
		document.write(res);
	},
	error: function (msg) {
		console.log("失败:" + msg); // 返回的错误信息
	}
});

$.ajax({
    url: 'main.htm',
    success: function(res){
        document.write(res);
    }
});

以上就是一些实现方法,可自适配.

转载请注明出处!

猜你喜欢

转载自blog.csdn.net/weixin_42614447/article/details/86529032