[英]Appending query strings to URL
"Appending an url with appendages that were already in the previous url?"
“在网址中附加上一个网址中已经存在的附件?” That's the most confusing title -- I know.
那是最令人困惑的标题-我知道。 But I can't think of a better way to explain it.
但是我想不出更好的方法来解释它。
Maybe the below example will help. 也许下面的例子会有所帮助。
I have URL 1: http://example.com/?value=xyz&stuff=abc
我有网址1:
http://example.com/?value=xyz&stuff=abc
: http://example.com/?value=xyz&stuff=abc
If someone clicks on a link within the page, can I pass along the appended values? 如果有人单击页面内的链接,我可以传递附加值吗?
ie: http://www.example.org/?value=xyz&stuff=abc
即:
http://www.example.org/?value=xyz&stuff=abc
: http://www.example.org/?value=xyz&stuff=abc
value = http://www.example.org/?value=xyz&stuff=abc
stuff = http://www.example.org/?value=xyz&stuff=abc
Thanks and sorry for being such a noob. 感谢和抱歉成为这样的菜鸟。
These appendages you ask about is the actual so called query part of an URI: 您询问的这些附件是URI的实际所谓查询部分:
<scheme>://<authority><path>?<query>
foo://example.com:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment
Taken from: 3. Syntax Components (RFC 3986) http://tools.ietf.org/html/rfc3986#page-16
摘自: 3.语法组件 (RFC 3986) http://tools.ietf.org/html/rfc3986#page-16
You then need a helper function that is append the (optional) <query>
to an existing <scheme>://<authority><path>
part. 然后,您需要一个辅助函数,该函数会将(可选)
<query>
附加到现有的<scheme>://<authority><path>
部分。 I ignore the <fragment>
for this exmaple, as it would needed to be added at the end and I want to leave something as an exercise: 我忽略了这个
<fragment>
的<fragment>
,因为需要在末尾添加它,而我想保留一些内容作为练习:
function href_append_query($href)
{
$query = isset($_SERVER['QUERY_STRING'])
? '?' . $_SERVER['QUERY_STRING']
: ''
;
$query = strtr(
$query, [
'"' => '"',
"'" => ''',
'&' => '&'
]
);
return $href . $query;
}
And it's usage: 它的用法是:
<a href="<?=href_append_query('http://step2.com/')?>Some link</a>
This little function will ensure that the exisitng QUERY_STRING
which can be obtained via $_SERVER
Docs is encoded for HTML output. 这个小功能将确保可以通过
$_SERVER
Docs获取的现有QUERY_STRING
被编码为HTML输出。
If links are built with PHP, just append the original query string to them: 如果链接是用PHP构建的,只需将原始查询字符串附加到它们:
<a href="http://step2.com/?<?php echo $_SERVER['QUERY_STRING']; ?>">Some link</a>
In JavaScript, just append the value of window.location.search
to all the links you need to 'pass' the query string. 在JavaScript中,只需将
window.location.search
的值附加到“传递”查询字符串所需的所有链接上即可。
You could also use js/jquery to append the existing query string to each link on the page: 您还可以使用js / jquery将现有查询字符串附加到页面上的每个链接:
$(function() {
$('a').each(function() {
link = $(this).attr('href');
query = window.location.search;
if (link.indexOf('?') !== -1 && query !== '') {
query = query.replace('?','&');
}
$(this).attr('href',link+query);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.