views:

96

answers:

4

How would i be able to put the follow on the end of every link of my website with out editing every link?

e.g www.WebsiteName.com/?ref=123

so if i went to www.WebsiteName.com/aboutus.php i want it to add ?ref=123 onto the end of the url.

+3  A: 

Depends on what you mean by without editing every link.

If you mean that you just don't want to manually add it in your source, you could do this:

$('a[href]').attr('href', function(i, hrf) { return hrf + '?ref=123';});

Or if you meant that you didn't want to have to do that, you could attach a .click() handler that will add the value when the link is clicked.

$('a[href]').click(function( e ) {
    e.preventDefault();
    window.location = this.href + '?ref=123';
});
patrick dw
what about if the link is already like: www.websitename.com/?xpto=123 ?? :-)
acmatos
thats alot for that goto wait 7 minute before accepting your answer
Gully
Well, I'm sticking the actual requirements in the question. We could change the question and then come up with different answers all day.
patrick dw
A: 
$('a[href]').attr("href", function(){
    this.href + '?ref=123';
});

Would be a very simple method. You'd probably have to add some error checking to make sure you're not ruining any other parameters, etc.

Justin Niessner
+1  A: 

I would use a RewriteRule. It's the easiest and most reliable way in my opinion. That is to say not parsing the page with PHP or fighting DOM load issues with JavaScript

Here's an example:

# For blank query only
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)\.php$ /$1.php?ref=123 [L]

# Append to existing query
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^(.*)$ /$1&ref=123 [QSA,L]
Jason McCreary
+7  A: 
var has_querystring = /\?/;

$("a[href]").
    each(function(el) {
        if ( el.href && has_querystring.test(el.href) ) {
            el.href += "&ref=123";
        } else {
            el.href += "?ref=123";
        }
    });
Sean Vieira
+1 this is what i was going to answer
Mouhannad
Exactly, you needto check whether the link already has query strings, although I would probably refactor that code a little, and use indexOf() instead. +1
KennethJ
@Kenneth - You only *need* to check if you know it is a possibility that one may exist. It would be wasteful to check if you know they will never be provided from the source.
patrick dw
@patrickdw yes true, but by that concept I should never capture a FileNotFound exception ;)
KennethJ
@Kenneth - Fair enough. :o) But by that reasoning it is unexpected, and should probably be replaced rather than extending a querystring that we're not expecting.
patrick dw
@patrickdw You are right, if you know that no URL on your website will have a query string, ever, then you don't have to check. However, chances are it does, or will have, at some point in the future. When that time comes, you or the next maintainer will have an annoying head-scratching bug on your hands. A bug where, if I was the one who found it in code I didn't write, would result in a very loud WTF upon realisation what was going on :)
KennethJ
@Kenneth - Indeed. A bug could manifest itself whether you ignore, replace or extend, which means the only real answer should be *do it at the source instead of in javascript*.
patrick dw
@patrickdw, it's very easy that someone would add query parameters to a link in the future, should keep the check. However, indexOf is faster. Also, a client side solution for this? It may be the easiest way but relies on JS enabled. I would create a server side createLink method that does this for you, yeah replace all your links. That could also be used to maintain the session id if user doesn't have cookies enabled.
Juan Mendes
@Juan - I don't disagree. :o) My main point has been that *any* change to the anticipated source being received from the server could lead to unexpected results in the javascript execution. This should *definitely* be done server side. Yet the asker wanted to know simply how to add to the href on the client side. We can always break client-side solutions by changing the parameters of the question. I could break this solution by adding the requirement to bypass the element completely if a query parameter exists. :o)
patrick dw
@patrickdw @Juan Yup, a server-side solution is definitely preferable for this, no question :) Either way, that bit of JS will do the trick if you for one reason or another you don't have control over the source.
KennethJ
@Kenneth - Given certain assumptions, yes, it could be that it would do the trick. The fact is that if you don't have control over the source, you can't know if this, or any other answer here (including mine), is appropriate. (At least not without more info.) This brings us back to the original question posted. And this is exactly what I mean when I say that we could be here all day coming up with solutions as long as we find different ways to modify the question.
patrick dw
@patrickdw As the hitchhiker's guide says: people are a problem ;)
KennethJ
@Kenneth - Ah yes, something upon which we can fully agree. :o)
patrick dw