Using url to pass parameters should be familiar to everyone, for example:
http://www.softwhy.com/home.php?mod=space&do=home&view=all
Since parameters are passed, it is natural to obtain the passed parameters. Of course, there are many ways to obtain parameters. Here is one of them, and I will share it with you. I hope it can bring you some help. Code As follows:
var url="http://www.softwhy.com/home.php?mod=space&do=home&view=all"; if(url.indexOf("?")!=-1) { var str=url.substr(url.indexOf("?")+1); strs=str.split("&"); for(i=0;i<strs.length;i++) { alert(strs.length); alert(strs[i].split("=")[0]); alert(strs[i].split("=")[1]); alert(strs[i].split("=")[0],'=',strs[i].split("=")[1],'<br>'); } }
In the above link:
The length of 1.strs.length is 3.
2.strs[0].split("=")[0] is mod, strs[0].split("=")[1] is space.
3.strs[1].split("=")[0] is do, strs[1].split("=")[1] is home.
The following are analogous.
Let me share with you 3 JS code examples for obtaining address bar parameters
Sometimes, we need to get the address bar parameter value of a static page. Using JS is the easiest way. Here are three function codes that use javaScript to get the address bar parameter. They are relatively easy to use. Let’s share them with you one by one. Share code:
JS gets the address bar string parameters, method one:
<script type="text/javascript"> Request = { QueryString : function(item){ var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i")); return svalue ? svalue[] : svalue; } } alert(Request.QueryString("id")); </script>
JS gets the address bar string parameters, method two:
<script type="text/javascript"> var URLParams = new Array(); var aParams = document.location.search.substr().split('&'); for (i=; i < aParams.length i++){ var aParam = aParams.split('='); URLParams[aParam[]] = aParam[]; } //获取传过来的name参数: name=URLParams["name"]; </script>
JS gets the address bar string parameters, method three:
<script type="text/javascript"> function getvalue(name) { var str=window.location.search; if (str.indexOf(name)!=-) { var pos_start=str.indexOf(name)+name.length+; var pos_end=str.indexOf("&",pos_start); if (pos_end==-) { return str.substring(pos_start); } else { return str.substring(pos_start,pos_end) } } else { return "没有这个name值"; } } var strName=prompt("请输入您所要值的名字"); alert(getvalue(strName)); </script>
These three methods use different cores, please choose according to your own program requirements.