SoFunction
Updated on 2025-02-28

Example details how to get link parameters in JavaScript

You should be familiar with using url to pass parameters, for example:

/?mod=space&do=home&view=all

Since you pass parameters, you will naturally need to obtain the passed parameters. Of course, there are many ways to obtain parameters. Here are one of them and share them with you. I hope it can bring some help to you. The code is as follows:

var url="/?mod=space&do=home&view=all";
if(("?")!=-1) 
{
var str=(("?")+1);
strs=("&");
for(i=0;i<;i++) 
{
alert();
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 is 3.
[0].split("=")[0] is mod, and strs[0].split("=")[1] is space.
[1].split("=")[0] is do, and strs[1].split("=")[1] is home.

The following order and so on.

Below I will share with you three code examples of JS to obtain address bar parameters

Sometimes, we need to obtain the address bar parameter value of a static page. Using JS is the easiest way. The following collects three function codes for using javascript to obtain address bar parameters, which are easier to use. Let’s share the code with you one by one:

JS gets the address bar string parameters, method 1:

<script type="text/javascript">
Request = {
QueryString : function(item){
var svalue = (new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
return svalue ? svalue[] : svalue;
}
}
alert(("id"));
</script>

JS gets the address bar string parameters, method two:

&lt;script type="text/javascript"&gt;
var URLParams = new Array();
var aParams = ().split('&amp;');
for (i=; i &lt;  i++){
var aParam = ('=');
URLParams[aParam[]] = aParam[];
}
//Get the passed name parameter:name=URLParams["name"];
&lt;/script&gt;

JS gets the address bar string parameters, method three:

&lt;script type="text/javascript"&gt;
function getvalue(name)
{
var str=;
if ((name)!=-)
{
var pos_start=(name)++;
var pos_end=("&amp;",pos_start);
if (pos_end==-)
{
return (pos_start);
}
else
{
return (pos_start,pos_end)
}
}
else
{
return "Not this name value";
}
}
var strName=prompt("Please enter the name of the value you want");
alert(getvalue(strName));
&lt;/script&gt;

The kernels used by these three methods are different. Please choose to use them according to your own program requirements.