简体   繁体   中英

How to get variable's value from URL and pass to all the links on the whole site? but ignore it if you just visit the home page

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM