1

I am aware that I can access all external links on a page using something like:

// Get external links
$.expr[':'].external = function(obj){
return !obj.href.match(/^mailto\:/)
        && (obj.hostname != location.hostname);
};

does anyone know how I can add a query string pair (along the lines of ?aid=1234567) to the URLs in the external links?

Thanks in advance

1 Answer 1

2

Something like this maybe?


// custom :external selector
$.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/)
            && (obj.hostname != location.hostname);
};

// put the new querystring pair on every external link
$("a:external").each(function() {
     $(this).attr("href", $(this).attr("href") + "?aid=1234567");
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the prompt reply. Didn't work at first until I put it in the $(document).ready() function, and now it works a treat. Many thanks
Sorry, I guess I just made the assumption that you knew to put it there, but thank you for accepting.
my fault - should have said that I was new to jquery Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.