admin管理员组文章数量:1192566
I'm looking for a robust way of dynamically injecting a query string parameter when a visitor clicks on a static anchor-link.
For example a link:
<a href="">Test</a>
I want to pass a query string to the next page but have to assign the value dynamically. How can I achieve this?
I know this is simple I'm just missing something obvious! =\
Thanks in advance,
John d
I'm looking for a robust way of dynamically injecting a query string parameter when a visitor clicks on a static anchor-link.
For example a link:
<a href="http://www.johndoeslink.com">Test</a>
I want to pass a query string to the next page but have to assign the value dynamically. How can I achieve this?
I know this is simple I'm just missing something obvious! =\
Thanks in advance,
John d
Share Improve this question asked Aug 13, 2014 at 15:32 krexkrex 3453 gold badges8 silver badges21 bronze badges2 Answers
Reset to default 19There are many ways you could do this. Below I've listed two very simple methods. Here I'm assuming you already have a reference to your a
element (here I've called this element
):
Modifying the href
attribute
element.href += '?query=value';
Using a click event listener
element.addEventListener('click', function(event) {
// Stop the link from redirecting
event.preventDefault();
// Redirect instead with JavaScript
window.location.href = element.href + '?query=value';
}, false);
If you already know the value to append when loading the page then you can add this when the document is ready : If you're using JQuery use this:
$( 'class' ).attr( 'href', function(index, value) {
return value + '?item=myValue';
});
Otherwise (plain Javascript):
var link = document.getElementById("mylink");
link.setAttribute('href', 'http://www.johndoeslink.com'+'?item=myValue');
本文标签: javascriptAdd QueryString parameter to static link on clickStack Overflow
版权声明:本文标题:javascript - Add Query-String parameter to static link on click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738433695a2086548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论