在这里,我遇到了一个场景,我想在重定向之前编辑 anchor 标记
的href
。我的情况是,如果 anchor 标记
的href
属性不包含QueryString
,则将上一页查询字符串附加到href
意味着,假设我当前的页面网址是 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 - 重定向前编辑 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22529655/