通过链接、按钮、菜单等附加查询字符串



我在Wordpress网站上使用这个javascript已经有一段时间了,它一直运行良好,直到一两天前,我没有更改任何会影响它的内容。它在我的测试站点上仍然运行良好,所以我不知道它为什么停止工作。我正在寻找解决这个问题的办法。基本上,代理商会得到一个唯一的查询字符串,这样当他们的客户访问网站时,他们就会获得佣金。当访问者从一页转到另一页或单击报价按钮(转到外部门户(时,查询字符串将逐页传递到外部门户。site.com/?group=agent123-脚本正在加载。Firefox错误为href未定义。Chrome显示了关于indexOf的错误,下面的行修复了错误,但没有使脚本工作。。。我不是写剧本的,所以我想不通。

var index = window.location.href.indexOf('?') 
if(index != -1){
var querystring = window.location.href.slice(index + 1)
var tagA = document.getElementsByTagName('a');
// this next line fixed the error hence allowing other 
// broken scripts after this to work again but script does not work 
// for appending the query strings...   
if (href !== undefined)

for(var i = 0; i < tagA.length; i++){
var href = tagA[i].getAttribute('href');
href += (href.indexOf('?') != -1)? '&' : '?';
href += querystring;
tagA[i].setAttribute('href', href);


}
}

只需在声明变量后在for(var i = 0; i < tagA.length; i++){中移动if (href !== undefined)

for(var i = 0; i < tagA.length; i++){
var href = tagA[i].getAttribute('href'); 
if (href !== undefined) {
href += (href.indexOf('?') != -1)? '&' : '?';
href += querystring;
tagA[i].setAttribute('href', href);
}

}
}

致以最良好的问候。

最新更新