1

I am trying to get a variable from a URL, for example if the users can link to this page: www.example.com?var1=blabla&var2=blabla and there are many links on this site www.example.com. What I want is no matter where my users click, they will always see the link appended with 2 variables they got from this link www.example.com?var1=blabla&var2=blabla for example: if there is another link

<a href="www.example.com/page1.php"/>Go to page1</a>

on www.example.com?var1=blabla&var2=blabla, they will go to page www.example.com/page1.php?var1=blabla&var2=blabla.
This is my code:

<script type="text/javascript">
$(document).ready(function() {
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1);
    var links = $('a');
    links.each(function(){
    var curLink=$(this);
    var href = curLink.attr('href');
    var newhref = href +'?'+ hashes; 
    curLink.attr('href',newhref);
    });
});
</script>

My question is: I can only make all the links on the same page append those 2 variable. If the users go to another page, it won't do the same thing, because there is no script on it... but I can't add same script on every pages... And another question, I want to clear the things after question mark and append the new ones.. is that possible to do that?

Thanks!

peipei
  • 1,407
  • 3
  • 14
  • 22
  • None of the answers (so far) address what will happen if one page contains a form which needs to GET / POST to another page (presumably these GET parameters originate in a form to begin with). If you have the option you could try and get these persisted variables in a server-side session object rather than relying on the client to carry them around. – tomfumb Nov 08 '11 at 22:49
  • @tomfumb - no where did he say he was using forms. From what I can tell he is only trying to modify links. Agreed that this is best solved server-side, but I had a feeling he doesn't have access or he would be able to add scripts to all his pages. So it wasn't worth getting in to. – mrtsherman Nov 08 '11 at 23:09
  • @mrtsherman that's why I said 'if' :) Without meaning to be disparaging, given the OP's apparent unfamiliarity with handling parameters and URLs it wouldn't surprise me if the site does include forms but the OP didn't know / think it worth mentioning – tomfumb Nov 08 '11 at 23:14
  • @tomfumb - good thought. no offense here! Hey peipei, are you using forms? – mrtsherman Nov 08 '11 at 23:22
  • @tomfumb I am not using form. I am just trying to modify all the links on the site. Append every links 2 new variables. But before append, need to clear after question marks. like mrtsherman wrote below: window.location.href.substring() . I am thinking use cookies to store those 2, I am still trying. Thanks guys!!! ^^ – peipei Nov 08 '11 at 23:37
  • @mrtsherman I am not using form. I am just trying to modify all the links on the site. Append every links 2 new variables. But before append, need to clear after question marks. like you wrote below: window.location.href.substring() . I am thinking use cookies to store those 2, I am still trying. Thanks guys!!! ^^ – peipei Nov 08 '11 at 23:37

1 Answers1

1

Your code looks good to me. And from your question it appears that your problems lie outside this.

My question is: I can only make all the links on the same page append those 2 variable. If the users go to another page, it won't do the same thing, because there is no script on it... but I can't add same script on every pages...

If you can't run your script on every page then you can't modify the url's. You will need to do it server side. If you can't do it server side then you are out of luck.

And another question, I want to clear the things after question mark and append the new ones.. is that possible to do that?

Yes, that is certainly possible. To strip the querystring use the following code.

var strippedHREF = window.location.href.substring(0, window.location.href.indexOf('?'));
mrtsherman
  • 39,342
  • 23
  • 87
  • 111