Html 如何将 url 参数添加到当前 url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8562613/
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 add url parameter to the current url?
提问by ilhan
Currently I'm at
目前我在
http://example.com/topic.php?id=14
and I want to make a link to
我想建立一个链接
http://example.com/topic.php?id=14&like=like
by not defining the current url. Like <a href="&like=like">Like</a>
. However this last one shows me http://example.com/&like=like
通过不定义当前 url。喜欢<a href="&like=like">Like</a>
。然而这最后一个向我展示了http://example.com/&like=like
采纳答案by Quentin
There is no way to write a relative URI that preserves the existing query string while adding additional parameters to it.
无法编写相对 URI 来保留现有查询字符串,同时向其中添加其他参数。
You have to:
你必须:
topic.php?id=14&like=like
回答by Marco Pace
function currentUrl() {
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$script = $_SERVER['SCRIPT_NAME'];
$params = $_SERVER['QUERY_STRING'];
return $protocol . '://' . $host . $script . '?' . $params;
}
Then add your value with something like;
然后用类似的东西添加你的价值;
echo currentUrl().'&value=myVal';
回答by Zachary Weixelbaum
I know I'm late to the game, but you can just do ?id=14&like=like
by using http build query as follows:
我知道我迟到了,但是您可以?id=14&like=like
通过使用 http 构建查询来完成,如下所示:
http_build_query(array_merge($_GET, array("like"=>"like")))
Whatever GET parameters you had will still be there and if like
was a parameter before it will be overwritten, otherwise it will be included at the end.
无论您拥有什么 GET 参数,它仍然会在那里,如果like
是参数,它将被覆盖,否则它将被包含在最后。
回答by mrts
In case you want to add the URL parameter in JavaScript, see this answer. As suggested there, you can use the URLSeachParams
API in modern browsers as follows:
如果您想在 JavaScript 中添加 URL 参数,请参阅此答案。按照那里的建议,您可以URLSeachParams
在现代浏览器中使用API,如下所示:
<script>
function addUrlParameter(name, value) {
var searchParams = new URLSearchParams(window.location.search)
searchParams.set(name, value)
window.location.search = searchParams.toString()
}
</script>
<body>
...
<a onclick="addUrlParameter('like', 'like')">Like this page</a>
...
</body>
回答by span
If you wish to use "like" as a parameter your link needs to be:
如果您希望使用“like”作为参数,您的链接需要是:
<a href="/topic.php?like=like">Like</a>
More likely though is that you want:
更有可能的是你想要:
<a href="/topic.php?id=14&like=like">Like</a>
回答by killernova
Maybe you can write a function as follows:
也许你可以写一个函数如下:
var addParams = function(key, val, url) {
var arr = url.split('?');
if(arr.length == 1) {
return url + '?' + key + '=' + val;
}
else if(arr.length == 2) {
var params = arr[1].split('&');
var p = {};
var a = [];
var strarr = [];
$.each(params, function(index, element) {
a = element.split('=');
p[a[0]] = a[1];
})
p[key] = val;
for(var o in p) {
strarr.push(o + '=' + p[o]);
}
var str = strarr.join('&');
return(arr[0] + '?' + str);
}
}
回答by Kamil Kie?czewski
It is not elegantbut possible to do it as one-liner <a>
element
它不优雅但可以作为单行<a>
元素来做
<a href onclick="event.preventDefault(); location+='&like=like'">Like</a>