Get variables from URL with javascript

Share the love!
Get variables from URL with javascript

Hello friends, this time we have a post of Javascript which i love most well this time we will see how we can get variables from URL using Javascript. Now if you don't know what are variables then let me tell you variable works like a container to store a value to get by defining that variable like for example

var a = "blog.";
var b = "";
var c = a + b;
//now when you will define c the result will be -

Ohk so what above i has done is i have maded a variable as a and define its value as blog and same for b and the value of b is and for c i have added value which will add the value of a and b a + b so when will you define c the return will be that was an example of variables but remember that they are case-sensitive so remember when creating a variable this small mistake make you crazy sometimes. ohk before going to next if you haven't read my previous post just have a look it was about - How to remove outline from hyper links, textarea and input fields

What is URL variable?

so going to next what is URL variable and what is the use? well it is a very useful and helpful part which allows you to send your value to next page like the best it work with php or any server language but we can use it with javascript also but the best result will be with a server based language so now what we can do with it like we can change the styles, count page-views send data to another page(the most popular and important) etc. Now let's understand how they work in URL or how put them into URL. this is how it will be placed now user is a variable and its value is wali. So this was how we can pass a value of a variable with URL but what if i want to get more like i want to send more variables like user=wali and id=1 then you will simply put & like

Get variable from URL

Now the main thing how to get that variable value? well for that we need to add a small function which will get your url variables. Lets See this-

function geturlvar(vars){
var wl =;
var v = wl.split("&");
for (var i=0;i<vars.length;i++) {
var p = v[i].split("=");
if(p[0] == vars){return p[1];}

Just include the above function once in your page and to know the value just simply call it like the below example-

//URL Query

//The below line will give you the value of the user variable

//Likewise to know the value of id do the same.

All done!

So this was how we can get a variable from URL and submit or get a value trough it using javascript! it was a short post but this thing can do a lot of things and the last 5 mins of this year lol! Wish you A very Happy new year! Keep visiting!

Comment with-

Disqus Blogger Google Facebook


Post a Comment

Comments :