jQuery 如何使用jquery获取上一页的url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2415633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get previous page url using jquery
提问by Geeth
How to get the previous page url using jquery?
如何使用jquery获取上一页的url?
I am using the following code to get the current page location
我正在使用以下代码获取当前页面位置
$(document).ready(function() {
var pathname = window.location.pathname;
});
回答by Marcos Placona
Easy as pie.
非常简单。
$(document).ready(function() {
var referrer = document.referrer;
});
Hope it helps. It is not always available though.
希望能帮助到你。但它并不总是可用。
回答by Grumpy
Do you mean something like history.go(-1);
? It will go back to the previous page.
你的意思是这样的history.go(-1);
吗?它将返回上一页。
回答by Amarghosh
var from = document.referrer;
console.log(from);
document.referrer
won't be always available.
document.referrer
不会一直可用。
回答by Roel B
If you are using PHP, you can check previous url using php script rather than javascript. Here is the code:
如果您使用的是 PHP,则可以使用 php 脚本而不是 javascript 检查以前的 url。这是代码:
echo $_SERVER['HTTP_REFERER'];
Hope it helps even out of relevance :)
希望即使不相关也能有所帮助:)
回答by Spidi's Web
We have document.referrer for this, but it is not on which we can relay. This could be saved, could be not
我们有用于此的 document.referrer,但我们无法在其上进行中继。这可以保存,可以不
$(document).ready(function() {
var referrer = document.referrer;
});
The better approach is to add a cookie to store the previous-url in the browser.
更好的方法是添加一个cookie来存储浏览器中的previous-url。
回答by Praveen Kumar Sharma
simple & sweet
简单甜美
window.location = document.referrer;
回答by Sameera
$(document).ready(function() {
var referrer = document.referrer;
if(referrer.equals("Setting.jsp")){
function goBack() {
window.history.go();
}
}
if(referrer.equals("http://localhost:8080/Ads/Terms.jsp")){
window.history.forward();
function noBack() {
window.history.forward();
}
}
});
using this you can avoid load previous page load
使用它可以避免加载上一页加载
回答by mspapant
document.referrer is not working always.
document.referrer 并不总是有效。
You can use:
您可以使用:
window.location.origin