Reputation: 5856
I have the follwoing jquery which opens all external links in a new window:
$("a[href^='http']").not("[href*='<?php bloginfo('url');?>']").attr('target','_blank');
This will show somehting like this:
<a href="http://www.example.com" target="_blank">Link</a>
However i would like to adapt this and add a variable to the end of the url for tracking purposes. So it will look something like this:
<a href="http://www.example.com?myvarhere=true" target="_blank">Link</a>
Upvotes: 0
Views: 219
Reputation: 59282
Of course, you can do it
var myVariable = "some string";
$("a[href^='http']").not("[href*='<?php bloginfo('url');?>']")
.attr('target','_blank').attr('href', function(_, h){
return h + "?" + myVariable + "=true";
});
Upvotes: 1
Reputation: 388436
You can use the .attr() version that takes a callback function as its argument
$("a[href^='http']").not("[href*='<?php bloginfo('url');?>']").attr('target', '_blank').attr('href', function (i, href) {
return href + '?somevar=somevalue'
});
Note: The callback implementation may have to be more complected if you want to check whether the url already has another parameter if so you will have to use &
to append the new param
Upvotes: 2