javascript - 使用history.back click获取查询字符串

标签 javascript jquery html browser-history

我有一个小要求,即将查询字符串从 demo1.html 传递到 demo2.html。 在 demo2.html 中,有后退按钮(history.back()),因此当用户单击后退按钮时,它将重定向到 demo1.html。在此 demo1.html 中,我想使用 javascript 或 jquery 获取查询字符串值或上一页 url。

请找到下面的演示 html 脚本供您引用。

我需要 demo1 中 demo2 页面的查询字符串值

demo1.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<script>
function display()
{
    var wind =  document.referrer;
    var preUrl = window.location.protocol;
    alert(wind);alert(preUrl);
}
</script>
<body>
Search Content of the document......
<a href="demo2.html?email=shiva@gmail.com">Search</a>
<a href="#" onclick="display()">Display</a>
</body>

</html>

demo2.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<script>
function goBack() { 
    window.history.back();
    return false;
}
</script>
<body>
demo 2 Content of the document......
<a href="#" onclick="goBack()">Back</a>
</body>

</html>

雷兹
湿婆

最佳答案

将查询字符串保存在充当后退按钮的 href 中,然后使用 location.replace()

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>    
<body>
demo 2 Content of the document......
<a href="demo1.html?querystring-on-demo2-appended-here" onclick="location.replace(this.href); return false">Back</a>
</body>

</html>

关于javascript - 使用history.back click获取查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23776721/

相关文章:

java - 如何在 google api 图表的堆积柱形图中使用条形底部的注释显示轴值?

javascript - 使用ES6 Class语法,如何实现其他文件中的类方法?

Javascript 循环遍历对象数组

javascript - jQuery 在点击时隐藏多个 div

javascript - jQuery - 重定向到页面后更改页面的属性

javascript append onclick 函数未定义

android - 将 Appcloud 与 Jquery Mobile 结合使用

javascript - 在多个位置循环显示一个 Sprite 或多个重复图像

html - rails 3/HAML : How can I output haml raw from a variable?

html - 文本省略优先级