I would like to append a query string onto all dynamic links within a page - to fix a bug in a old release - is this possible?
Any ideas?
Something like this?
var querystring = 'myquerystringtoadd';
$('a').each(function() {
var href = $(this).attr('href');
if (href) {
href += (href.match(/\?/) ? '&' : '?') + querystring;
$(this).attr('href', href);
}
});
Working example.
This solution with native javascript :
var querystring = 'yourQueryStringHere=;-)';
document.addEventListener('click', function (e) {
var x = e.originalTarget;
if (x.nodeName === 'A') {
var href = x.getAttribute('href');
if(href) {
href += (/\?/.test(href) ? '&' : '?') + querystring;
x.setAttribute('href', href);
}
}
}, false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With