JS取得URL中的参数
方法1:
<script>
var url = "?aa=1&w=2&cc=3&ss=我要";
function getUrlParam(url, paramName ){
var oRegex = new RegExp('[?&]' + paramName + '=([^&]+)', 'i' ) ;
var oMatch = oRegex.exec( url ) ; if ( oMatch && oMatch.length > 1 )
return decodeURIComponent( oMatch[1] ) ;
else
return '' ;
}
alert(getUrlParam(url,"aa"));
alert(getUrlParam(url,"w"));
alert(getUrlParam(url,"ss"));
</script>
方法2:
<script>
var str = "http://localhost/aaa.aspx?aa=1&bb=2&cc=3&dd=哈哈";
alert(getUrlParam(str,"aa"));
alert(getUrlParam(str,"dd"));
function getUrlParam(url,paramName){
var reg = new RegExp(".*?" + paramName + "=([^&]*)?&.*?$"+"|.*?"+paramName+"=([^&]*)?$|");
if(reg.exec(url) == url) return '';
return url.replace(reg,"$1$2");
}
</script>
<script>
var url = "?aa=1&w=2&cc=3&ss=我要";
function getUrlParam(url, paramName ){
var oRegex = new RegExp('[?&]' + paramName + '=([^&]+)', 'i' ) ;
var oMatch = oRegex.exec( url ) ; if ( oMatch && oMatch.length > 1 )
return decodeURIComponent( oMatch[1] ) ;
else
return '' ;
}
alert(getUrlParam(url,"aa"));
alert(getUrlParam(url,"w"));
alert(getUrlParam(url,"ss"));
</script>
方法2:
<script>
var str = "http://localhost/aaa.aspx?aa=1&bb=2&cc=3&dd=哈哈";
alert(getUrlParam(str,"aa"));
alert(getUrlParam(str,"dd"));
function getUrlParam(url,paramName){
var reg = new RegExp(".*?" + paramName + "=([^&]*)?&.*?$"+"|.*?"+paramName+"=([^&]*)?$|");
if(reg.exec(url) == url) return '';
return url.replace(reg,"$1$2");
}
</script>