【发布时间】:2014-03-20 10:12:16
【问题描述】:
在这里,我遇到了一个场景,我想在重定向之前编辑 href 或 anchor tag。我的情况是,如果anchor tag 的href 属性不包含QueryString,则将上一页查询字符串附加到href 意味着,假设我当前的页面url 是xyz?x=2&y=4
当前的href
href="local/services"
编辑后
href="local/services?x=2&y=4"
我试过了。
HTML:
<a href="http://localhost/services" onclick="appendString(this)">test link </a>
JavaScript
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1)
obj.setAttribute('href') = url + window.location.search;
}
也试过
function appendString(obj) {
var url = obj.getAttribute('href');
if (url.indexOf('?') != -1)
window.location.href = url + window.location.search;
}
但没有成功。 请提出解决方案。 谢谢。
【问题讨论】:
标签: javascript html query-string