Home > Web Front-end > JS Tutorial > body text

Detailed example of how to obtain link parameters in JavaScript_javascript skills

WBOY
Release: 2016-05-16 15:22:20
Original
1312 people have browsed it

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>');
}
}
Copy after login

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("[\&#63;\&]" + item + "=([^\&]*)(\&&#63;)","i"));
return svalue &#63; svalue[] : svalue;
}
}
alert(Request.QueryString("id"));
</script>
Copy after login

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>
Copy after login

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>
Copy after login

These three methods use different cores, please choose according to your own program requirements.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template