javascript 引用 URL 的查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19884803/
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
Query string of referrer URL
提问by gravityboy
Using php or javascript or regex, is there a quick (one-liner hopefully) to get the query string from the previous (referrer) URL?
使用 php 或 javascript 或正则表达式,是否有一种快速(希望是单行)从前一个(引用者)URL 获取查询字符串?
Example,
例子,
User is at
用户在
www.sample.com?one
Then clicks link to go to
然后点击链接进入
www.sample.com?two
From page two... I want to know the previous query string "one."
从第二页...我想知道上一个查询字符串“一”。
回答by plalx
This perhaps?
这也许?
var qs = document.referrer.split('?')[1] || '';
Looks like it's a contest :)
看起来这是一场比赛:)
var r = document.referrer,
indexOfQm = r.indexOf('?'),
len = r.length;
/(?:[^?]+)\??(.*)/.exec(r)[1];
r.split('?').shift().pop() || '';
r.slice(indexOfQm === -1? len : indexOfQm - len + 1);
r.substring(indexOfQm === -1? len : indexOfQm + 1);
r.replace(/^.+?(?:\?(.*)|$)/, '');
r.split('').slice(indexOfQm + 1).join('');
[].reduce.call(r, function (res, c) {
if (res.found) res.qs += c;
else if (c === '?') res.found = true;
return res;
}, { found: false, qs: '' }).qs;
回答by HaukurHaf
You need to grab the referrer string on the server side.
您需要在服务器端获取引用字符串。
If using PHP, it's as simple as this:
如果使用 PHP,就这么简单:
$referrer = $_SERVER['HTTP_REFERER'];
Then just split the string on the '?' sign to an array and you have the query-string at array index 1
然后只需在 '?' 上拆分字符串 签名到一个数组,你有数组索引 1 处的查询字符串
回答by rninty
Here's one one-liner per line:
这是每行一个单行:
document.referrer.split("?").slice(1).join("?")
document.referrer.substring(document.referrer.indexOf("?") + 1)
/(?:\?(.+))?/.exec(document.referrer)[1]
referrer
is not a good thing to rely on, though; people can and do turn them off. (I do.) Consider cookies or local storage.
referrer
然而,这不是一件值得依赖的好事情;人们可以并且确实将它们关闭。(我愿意。)考虑 cookie 或本地存储。