<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Js解析url参数方法</title>
</head>
<body>
<div>
测试url:http://www.webkfa.com?id=1<br/>
取id的值:<span id="testid"></span>
</div>
<script type="text/javascript">
function getparam(name,url){
var value="";
if(url.indexOf('?')>-1){
var arr=url.split("?");
if(arr[1].indexOf("&")>-1){
var temarr=arr[1].split('&');
for(var i=0;i<temarr.length;i++){
if(temarr[i].indexOf(name+'=')>-1){
value=temarr[i].split("=")[1];
}
}
}else{
if(arr[1].indexOf(name+'=')>-1){
value=arr[1].split('=')[1];
}
}
}
return value;
}
//正式代码
//var url=window.location.href;
//getparam("id",url);
//运行测试代码
var tmpid=getparam("id","http://www.webkfa.com?id=1");
document.getElementById("testid").innerHTML=tmpid;
</script>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Js解析url参数方法</title>
</head>
<body>
<div>
测试url:http://www.webkfa.com?id=1<br/>
取id的值:<span id="testid"></span>
</div>
<script type="text/javascript">
function getparam(name,url){
var value="";
if(url.indexOf('?')>-1){
var arr=url.split("?");
if(arr[1].indexOf("&")>-1){
var temarr=arr[1].split('&');
for(var i=0;i<temarr.length;i++){
if(temarr[i].indexOf(name+'=')>-1){
value=temarr[i].split("=")[1];
}
}
}else{
if(arr[1].indexOf(name+'=')>-1){
value=arr[1].split('=')[1];
}
}
}
return value;
}
//正式代码
//var url=window.location.href;
//getparam("id",url);
//运行测试代码
var tmpid=getparam("id","http://www.webkfa.com?id=1");
document.getElementById("testid").innerHTML=tmpid;
</script>
</body>
</html>