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