javascriptはurlパラメータを交替します.
1782 ワード
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<script>
function setUrlParam(oldurl,paramname,pvalue){
var reg = new RegExp("(\\?|&)"+ paramname +"=([^&]*)(&|$)","gi");
var pst=oldurl.match(reg);
if((pst==undefined) || (pst==null)){
return oldurl+((oldurl.indexOf("?")==-1)?"?":"&")+paramname+"="+pvalue;
}
var t=pst[0];
var retxt=t.substring(0,t.indexOf("=")+1)+pvalue;
if(t.charAt(t.length-1)=='&') retxt+="&";
return oldurl.replace(reg,retxt);
}
alert(setUrlParam("http://servername/virturlpath/index.asp","a4","cxz"));
alert(setUrlParam("http://servername/virturlpath/index.asp?p1=123&p2=aa&p3=hh&p4=1","a4","cxz",1));
alert(setUrlParam("http://servername/virturlpath/index.asp?a4=abc&p1=123&p2=aa&p3=hh&p4=1","a4","cxz"));
alert(setUrlParam("http://servername/virturlpath/index.asp?sa4=abc&p1=123&p2=aa&p3=hh&p4=1","a4","cxz"));
alert(setUrlParam("http://servername/virturlpath/index.asp?sa4=abc&p1=123&p2=aa&a4=hh&p4=1","a4","cxz"));
</script>
</head>
<body>
</body>
</html>