0

How do i set a page to append a urlVariable into its links?

for example, i have 3 different pages and all are linked into the same page, e.g.
receiver.html

first page link:

receiver.html?sender=1

second page link:

receiver.html?sender=2

third page link:

receiver.html?sender=3

when the first page is clicked it will send the user to receiver.html which has many outgoinglinks inside, and the script will append the variable into all its outgoing links depending on the three pages above?

receiver.html

outgoinglink.html?sender=1
outgoinglink2.html?sender=1
outgoinglink3.html?sender=1

and if second page is used, the receiver.html will append

?sender=2

on all its link inside and so on and so forth..

franticfrantic
  • 2,511
  • 3
  • 17
  • 14

1 Answers1

0

Well you could use the function shown in this post (http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript) to get your parameter

function getParameterByName( name )

    {
      name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regexS = "[\\?&]"+name+"=([^&#]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( window.location.href );
      if( results == null )
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
    }

var sender = getParameterByName('sender');

then attach what you need to all href:

$('a').each(function(){
    var href = $(this).attr('href');
    if (href.indexOf('&') !== -1){
        //if you have on & in the href you must use &
        $(this).attr('href', href+'&sender='+sender);
     }else{
        $(this).attr('href', href+'?sender='+sender);
     }
});
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • Thanks! i can also append additional urlVars by adding another variable into the getParameter by name, thus: `var sender = getParameterByName('sender');` `var pv = getParameterByName('pv');` and modifying the functionCall: `$(this).attr('href', href+'&sender='+sender+'&pv='+pv);` – franticfrantic Jun 30 '11 at 17:18
  • Yes of course, but you need to modify the else part in this way: `$(this).attr('href', href+'?sender='+sender'&pv='+pv);` so that tue url is valid! If this answer your question please accept the answer – Nicola Peluchetti Jul 01 '11 at 08:24