簡體   English   中英

如何從URL獲取變量的值並傳遞到整個站點上的所有鏈接? 但是如果您只是訪問主頁,則忽略它

[英]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

所以我試圖完成這樣的事情

如何從URL獲取變量的值並傳遞到整個站點上的所有鏈接?

但是我遇到的問題是,當您僅訪問主頁或任何沒有變量值的頁面時,它會使頁面上的所有鏈接看起來像這樣:

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

因此,它將整個網址添加為變量值,而我需要完成的就是您訪問網頁時

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

它會在整個網站上保留該變量值,但是如果您僅訪問www.example.com/page1.php,則不會看到任何變量值。

我需要在此代碼中進行什么更改才能擺脫此問題

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

感謝您的任何幫助

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM