Examples: how to obtain link parameters using JavaScript, and how to obtain link parameters using javascript
You should be familiar with using url to pass parameters, for example:
Http://www.softwhy.com/home.php? Mod = space & do = home & view = all
Since parameters are passed, we naturally need to obtain the passed parameters. Of course, there are many ways to obtain parameters. Here we will introduce one of them and share them with you, the Code is 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:
1. The length of strs. length is 3.
2. strs [0]. split ("=") [0] Is mod, and strs [0]. split ("=") [1] is space.
3. strs [1]. split ("=") [0] is do, and strs [1]. split ("=") [1] is home.
And so on.
We will share with you three examples of code for obtaining the address bar parameters in JS.
Sometimes, we need to get the address bar parameter value of the static page. JS is the easiest way to use. The following three function codes are collected for obtaining the address bar parameters using javaScript, which is easy to use, the following code is shared with you one by one:
JS to obtain the address bar string parameters, Method 1:
<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 obtains the address bar string parameters. Method 2:
<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 [];} // obtain the passed name parameter: name = URLParams ["name"]; </script>
JS obtains the address bar string parameters. Method 3:
<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 "No such name value";} var strName = prompt ("Enter the name you want "); alert (getvalue (strName); </script>
These three methods use different kernels. Please use them according to your program requirements.
Articles you may be interested in:
- How to obtain url parameters using Javascript
- Javascript method for obtaining url parameters [Regular Expression and string truncation]
- JS obtains the parameter value instance code of the URL hyperlink
- Four parameter transmission formats: URL, hyperlink, js, and form
- How to solve the garbled problem of passing Chinese parameters over the hyperlink in the JSP page