js获取url参数,并根据参数跳转
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=" http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>获取url指定参数并跳转</title> <script type="text/javascript"> //获取当前url?后 键值对集合指定键的值 function getParamValue(key) { var url=window.location.href; var paramsUrl = url.substring(url.indexOf("?")+1,url.length); var params=[]; if(paramsUrl.indexOf("&")!=-1) { params=paramsUrl.split("&"); } else { if(key==paramsUrl.split("=")[0]) return paramsUrl.split("=")[1]; else return null; } var result=new Object(); result.key = new Array(); result.value= new Array(); result.get =function(value) { for(var x=0;x<this.key.length;x++) { if(this.key[x]==value) { return this.value[x]; } } return null; } for(var x=0;x<params.length;x++) { var temp = params[x].split("="); result.key[x] = temp[0]; result.value[x]=temp[1]; } return result.get(key); } // 参数名是什么就get什么 xxxx.php?str=new&ih=5&id=5666 getParamValue("id") ->5666 //alert(getParamValue("id")); //下面这个拼接函数自己封装下. function toUrl(flag) { if(flag==null) return false; window.location = " http://www.abc.com/"+flag+".php"; } //自动跳转 toUrl(getParamValue("str")); //点击链接进行跳转的 function toUrl2() { flag = getParamValue("ll") if(flag==null) flag = 'sj'; window.location = " http://localhost/test/get2.php?key="+flag+""; } </script> </head> <body> <a href="javascript:()" onclick="toUrl2()">下载</a> </body> </html>