Forum Moderators: open

Message Too Old, No Replies

Updating Links Based on Referring URL?

Change a link based on a referring url

         

Icantwritecode

6:59 pm on Mar 5, 2009 (gmt 0)

10+ Year Member



Is it possible to write a code in javascript that will change a link based on the referring url? For example,

If someone clicks through to a site from Google Adwords, an external link in the code might be xyz.com?123

But if they come from Yahoo Search Marketing, the link would change to xyz.com?ABC

For example.

Thanks!

sandblocks

8:53 am on Mar 7, 2009 (gmt 0)

10+ Year Member



This is a job for jQuery. jQuery is easy to start using, just download the js library, reference it in your page like any other script, then you can start writing code like this:


var appendQS;
if ( document.referrer.indexOf('www.example.com')> 0 ) {
appendQS = '?from=example.com'
} else {
appendQS = '?from=somewhereelse'
}
$("a").each(function(i){
var newRef;
newRef = $(this).attr('href')
newRef = newRef + appendQS
$(this).attr('href', newRef)
});

Keep in mind, this is a simplified example. Without some proper checking you could break some links (like email links, or links that already have a query string).