tags:

views:

181

answers:

4

How do I add let's say something like ajax=1 to all links on my page with jquery. I will also need to check if the url has existing parameters. for example "http://mysite.com/index.php?pl=132" will have to become "http://mysite.com/index.php?pl=132&ajax=1" Also, if the link does not have any parameters, for example "http://mysite.com/index.php", it will become "http://mysite.com/index.php?ajax=1" I want to load the jquery script on document ready so all links are changed on page load.

Thanks.

+3  A: 

You could do something like this:

$(function() {
   $("a").attr('href', function(i, h) {
     return h + (h.indexOf('?') != -1 ? "&ajax=1" : "?ajax=1");
   });
});

On document.ready this looks at every <a>, looks at it's href, if it contains ? already it appends &ajax=1 if it doesn't, it appends ?ajax=1.

Nick Craver
Except for `/weird/page.aspx?`. Also, this won't work for named anchors.
SLaks
@SLaks - The problem is the initial rendering of the link in that case, not jQuery...
Nick Craver
AFAIK, that's a legal (but very unlikely) URL.
SLaks
@SLaks - I agree, I'm hoping since the OP has `mysite.com` they have rendering control over the links and wouldn't be doing that :) Maybe personal preference, but I just wouldn't want/like to have links in that fashion.
Nick Craver
It will still fail for named anchors, though.
SLaks
@SLaks - As would both of ours on something like `page.php#home`, but that isn't what the OP asked, he wants to append this to every link.
Nick Craver
tested the script out. named anchors fail. links like "mysite.com/wiki/something" dont get appended....
Priyo
@Priyo - This works for links like that, see it working here: http://jsfiddle.net/xDvEx/ Maybe you have something else going on in the page interfering?
Nick Craver
links which are coded tis way: <a title="The Canon of Medicine" href="/wiki/The_Canon_of_Medicine"> remain unchanged
Priyo
@Priyo - Those works too, see the updated example to include it: http://jsfiddle.net/xDvEx/4/ If these aren't working for you, something *else* is happening in your page.
Nick Craver
+1  A: 

Like this:

$(function() {
    $('a[href]').attr('href', function(index, href) {
        var param = "key=value";

        if (href.charAt(href.length - 1) === '?') //Very unlikely
            return href + param;
        else if (href.indexOf('?') > 0)
            return href + '&' + param;
        else
            return href + '?' + param;
    });
})
SLaks
tested the script out. named anchors fail. links like "mysite.com/wiki/something" dont get appended....
Priyo
+1  A: 

If you're interested in plugins, there's the jQuery Query String Object. This will allow you simple checking of parameters in the querystring, and if necessary the ability to add more, remove some, or edit others.

Jonathan Sampson
This isn't what he's looking for.
SLaks
@SLaks I disagree. This accomplishes what you offered, but wrapped in a plugin and less-verbose. You can check to see if a parameter exists, if not, add it, etc.
Jonathan Sampson
Can this modify existing hyperlinks?
SLaks
@SLaks Yes. I suppose I should say instead that *you* can modify existing links with it.
Jonathan Sampson
this looks real interesting. but how do I ask the plugin to change all links on page?
Priyo
@Priyo By cycling through all links on the page as demonstrated in the other answers here. You can do this via `$.each()` or by using a function on the `$.attr()` method to return a modified version of the link's url.
Jonathan Sampson
Can you please help with a code sample for my case?
Priyo
A: 

Taking what is above, this is the best way to concatenate parameters to links.

Also avoid link with href like href="Javascript:YourFunction();"

$(function(){                                      
   var myRandom = getRandom();       
   $('a[href]').each(function(i){
        var currHref = $(this).attr("href");
        var isAfunction = currHref.substring(0,10);              
        if(isAfunction == "javascript"){                
            $(this).attr("href",currHref);
        }else{
            if (currHref.charAt(currHref.length - 1) === '?')                           
                $(this).attr("href",currHref);
            else if (currHref.indexOf('?') > 0)                
                $(this).attr("href",currHref+"&rand="+getRandom());
            else                  
                $(this).attr("href",currHref+"?rand="+getRandom());
        }
   });               
});
function getRandom(){
    var randomnumber = Math.floor(Math.random()*10000); 
    return randomnumber;
}
Francisco Alvarez