【发布时间】:2011-12-24 23:11:11
【问题描述】:
如果当前页面 URL 在查询字符串中有一个参数“myid1”,对于我网页中具有类“重写”的每个链接,我希望将链接 href 的查询字符串替换为当前页面 URL 的查询字符串。
我正在使用以下代码:
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script type="text/javascript">
$(function() {
var requestid = new String(gup('myid1'));
if (requestid!=null&&requestid!="") {
$("a.rewrite").each(function() {
var href = $(this).attr("href");
href += "?myid1=" + requestid;
$(this).attr("href", href);
})
}
})
//gup taken from here:http://www.netlobo.com/url_query_string_javascript.html
function gup(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
</script>
<a href="http://www.otherdomain.com?someid=1234" class="rewrite">Hyperlink</a>
问题是 URS 的查询字符串被添加到链接中而没有删除现有的。如何解决?
另外,我如何允许多一个名为“myid2”的参数。提前感谢您的帮助。
【问题讨论】:
-
关于
myid2的问题的第二部分我不明白。 -
@jfriend00 我想添加另一个查询参数 (myid2),它应该与 myid1 完全一样。
-
我还是不明白。您将所有现有查询参数替换为
myid1。你想把myid2放在哪里? -
例如,如果我们去 test.html?myid2=9876 超链接应该变成otherdomain.com/?myid2=9876。现在这种变化只发生在 myid1 上。我希望它也发生在 myid2 上。
标签: javascript jquery html css url-rewriting