0

So I was trying to accomplish something like this

how to get variable's value from URL and pass to all the links on the whole site?

But the problem I ran into is when you just visiting the home page or any page without the variable value it makes all links on the page to look like this:

http://www.website.com/page1/?http://www.website.com/

So it adds the whole url as a variable value, and what I need to accomplish is when you visit page

www.example.com/page1.php?var1=blabla

it keeps that variable value throughout the website, but if you just visit www.example.com/page1.php you won't see any variable value.

what do I need to change in this code to get rid of this problem

    <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>

Thank you for any help

Community
  • 1
  • 1
Osiris
  • 1
  • 1
  • 3
  • http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-url-parameter ? – online Thomas Jan 04 '16 at 13:41
  • I've tried top 10 solutions from that page, and none of them passing the variable value to all links so if I visit page like this www.example.com/page1.php?var1=blabla links on that page doesn't contain ?var1=blabla – Osiris Jan 04 '16 at 14:01

1 Answers1

0
<script type="text/javascript">
    $(document).ready(function() {
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1);
        if ( hashes != "" ) {
          var links = $('a');
          links.each(function(){
            var curLink=$(this);
            var href = curLink.attr('href');
            var newhref = href +'?'+ hashes; 
            curLink.attr('href',newhref);
          });
        }
    });
</script>
RST
  • 3,899
  • 2
  • 20
  • 33
  • Still the same result, if I visit page without any variable I get links like this http://www.example.com/page1.php?http://www.example.com/page1.php – Osiris Jan 04 '16 at 14:06
  • So If I visit page http://www.website.com/page1/ links on that page will look like http://www.website.com/page1/?http://www.website.com/page1/ and if I visit page http://www.website.com/page1/?var1=blabla every link would look like http://www.website.com/link2/?var1=blabla, and that's how I want it, I just don't want to use the whole url as a variable like in first example – Osiris Jan 04 '16 at 14:15
  • All it should be doing is, if variable, add it to all links, if no variable, leave links untouched. Maybe you need to adjust the `if`-statement a little. What value does `hashes` have on a page whithout variables? – RST Jan 04 '16 at 14:24
  • Not so sure the javascript is doing that, could be something in your permalinks, or .htaccess. Did you check the value of `hashes`? Or put an `alert('inside loop');` inside the `if`-loop to see if the actions are performed. – RST Jan 04 '16 at 14:47
  • Sorry for a stupid question but how do I check the value of hashes? thank you for your time and help – Osiris Jan 05 '16 at 13:35