JS分析URL字符串,取得参数名,AJAX传参请求代码示例

时间:2022-04-10 19:49:34
//当前页面URL中参数分析函数,正则校验
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
} function zan()
{
//取得想要的参数值,getQueryString中的参数为String型,传入参数名为URL中参数名,即可获得
var docchannel = getQueryString("docchannel");
var docid = getQueryString("docid");
var xmlhttp;
if (window.XMLHttpRequest)
{// for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("zanspan").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","./manager/zan+1.jsp?t="+Math.random()+"&docid="+docid+"&docchannel="+docchannel+"",true);
xmlhttp.send();
}