网站推广ID号跟踪
xxx.html?referraluserid=123
referraluserid.js JS源文件
referraluserid的参数会自动绑定页面A标签
有时是Post 表单跳转时不传数会丢失,这时可以根据后台或其它方式重新打印到前台页面,让Js重新获取到
<script type="text/javascript">
var referraluserid = '<%=BackReferraluserid%>';// BackReferraluserid 为后台值绑定到Js变量
</script>
紧接着引用JS文件
<script type="text/javascript" src="/Scripts/referraluserid.js"></script>
document.write("<input type='hidden' id='referraluserid' name='referraluserid' />"); $(document).ready(function () { $("a").each(function () { if ($(this).attr("href") == "#" || $(this).attr("href") == "") { $(this).attr("href", "/"); } }); addUrlParse("referraluserid"); }); function getUrlParams() { var search = window.location.search; var tmparray = search.substr(1, search.length).split("&"); var paramsArray = new Array; if (tmparray != null) { for (var i = 0; i < tmparray.length; i++) { var reg = /[=|^==]/; var set1 = tmparray[i].replace(reg, '&'); var tmpStr2 = set1.split('&'); var array = new Array; array[tmpStr2[0]] = tmpStr2[1]; paramsArray.push(array); } } return paramsArray; } function GetQueryString(name) { var paramsArray = getUrlParams(); if (paramsArray != null) { for (var i = 0; i < paramsArray.length; i++) { for (var j in paramsArray[i]) { if (j.toLowerCase() == name.toLowerCase()) { return paramsArray[i][j]; } } } } return null; } // typeof addUrlParse == "function" ? addUrlParse("referraluserid") : ""; function addUrlParse(ruid) { var ruidValue = GetQueryString(ruid); if (typeof (referraluserid) != "undefined") if (referraluserid != null && referraluserid != undefined && typeof referraluserid != "object" && (ruidValue == null || ruidValue == "" || ruidValue == undefined)) { ruidValue = referraluserid; } if (ruid != "" && ruid != null && ruidValue != "" && ruidValue != null) { var aStr = ruid + "=" + ruidValue; var url = window.location.search; try { $("a").each(function (i) { var hrefstr = $(this).attr("href"); if (hrefstr != null && hrefstr != "" && hrefstr.indexOf("javascript:") < 0) { var jinghao = ""; if (hrefstr.indexOf("#") > 0) { var arr = hrefstr.split("#"); hrefstr = arr[0]; jinghao = arr[1]; } if (hrefstr.indexOf("#") == -1) { if (hrefstr.toLowerCase().indexOf(ruid + "=") == -1) hrefstr = hrefstr + (hrefstr.indexOf("?") > 0 ? "&" + aStr : "?" + aStr); if (jinghao != "") { hrefstr += "#" + jinghao; } $(this).attr("href", hrefstr); } } }); } catch (e) { alert(e); } } } // var referraluserid = typeof getReferraluserid == "function" ? getReferraluserid() : ""; function getReferraluserid() { return GetQueryString("referraluserid"); } //var and_href = typeof getReferraluserid_href == "function" ? getReferraluserid_href("?") : ""; function getReferraluserid_href(a) { var href = ""; var referraluserid = GetQueryString("referraluserid"); if (referraluserid != null && referraluserid != undefined && referraluserid != "") href = a + "referraluserid=" + referraluserid; return href; }