Emerson Maningo
Emerson Maningo

Reputation: 2279

How to append query string arguments to hyperlink URL using jQuery

I have a fully working jQuery code to select a URL matching a specified domain:

jQuery( document ).ready( function( $ ) {
    $('a').each(function() {

        var href = $(this).attr('href');


        if(this.hostname && this.hostname == 'example.com') 
        {                   
            $(this)
                .removeAttr('target')
                .attr('rel', 'nofollow')                
                .attr('title', href)
       }
    });
});

As you can see, it will remove the target attribute, add rel nofollow and set title to the URL value. Now I having issue on how to modify the above code to add another feature to append query string to the URL value (href value). Supposing I want to append the following query string arguments/values:

argument1='test1'
argument2='test2'

such that the final URL will look like:

http://example.com/?argument1=test1&argument2=test2

Or any page of the example domain like

http://example.com/any_page/?argument1=test1&argument2=test2

Is there a simple method on doing this without using jQuery plugins?

Upvotes: 3

Views: 3765

Answers (1)

Samuel Liew
Samuel Liew

Reputation: 79113

Check out jQuery.param() for Objects and Arrays

or .serialize() for form data

Example: http://jsfiddle.net/DqXYn/

Upvotes: 2

Related Questions