简体   繁体   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

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? 如何从URL获取变量的值并传递到整个站点上的所有链接?

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/ 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 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. 它会在整个网站上保留该变量值,但是如果您仅访问www.example.com/page1.php,则不会看到任何变量值。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从URL获取变量的值并传递到整个站点上的所有链接? - how to get variable's value from URL and pass to all the links on the whole site? 您如何从带有节点puppeteer的页面获取所有链接? - How do you get all the links from a page with node puppeteer? 如何将url参数传递/附加到页面上的所有href(链接)? - How to pass/append url parameter to all hrefs (links) on a page? 如何跨所有页面存储在主页中获得的变量值? - How to store a variable value obtained in home page across all pages? 如何从网站上获取所有的图标,by url - How to get all the favicon from the site, by url 页面下载时如何从网站URL获取所有JavaScript源链接 - How to get all javascript source links from a website url when the page download 如何使用javascript在网站的所有网页/内部链接中传递网址参数? - How to pass URL parameters across all pages/internal links in a site with javascript? 如何从html页面变量中的表单获取值 - How to get value from form in the html page's variable 如何获取页面上所有链接的属性值? - how can i get an attribute value of all links on a page? 如果有人手动尝试访问angularjs中网站的不同网址,如何重定向回同一页面(他已经在的页面) - How to redirect back to same page (the page he is already in) if someone manually try to visit different url of the site in angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM