Get URL Variables
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
Usage
Example URL:
http://www.example.com/index.php?id=1&image=awesome.jpg
Calling getQueryVariable("id") - would return "1".
Calling getQueryVariable("image") - would return "awesome.jpg".
Requests = {
QueryString : function(item){
var svalue = location.search.match(new RegExp(“[\?\&]” + item + “=([^\&]*)(\&?)”,”i”));
return svalue ? svalue[1] : svalue;
}
}
//usage
Requests.QueryString(“id”);
Great snippet, but change the curly quotes to double quotes on this one.
nice ! thanks.
Great work, this is a really nice bit of coding! As always, keep it up!
You ‘re right
Nice, really helpful thanks!!
Thanks Chris, totally useful script. Soo going to use that today :D
beauty!!!!!!!
Can i remove the .html extension from my website having a windows based hosting?
I dont know if the server has Isapi_rewrite installed
regards
i think that line = return (false) would be create a bug if your url get more than one variable. false can be break a loop and return just first var in your url..
i think that line should be remove..
regards,
nb : pardon me if i wrong :)
I think you might be mis-reading the brackets. After the for loop has searched all of the
vars
(name-value pairs) and hasn’t foundvariable
then this function returns false.Awesome! Thanks very much!
hi my friends sorry if i speak english but i can’t explain in french so
i got this one in one scripte i created it
page.html?nom=28/09/1986&from=hossa&too=hisok
and i want to get the name of variable nom and from and too
i tried many scripts but they don’t work for me
please can you tell me how to do it in javascript
thanks so much
I think that is what this post is about.
Just call the function defined above.
getQueryVariable("nom");
– would return “28/09/1986”.getQueryVariable("from");
– would return “hossa”.getQueryVariable("too");
– would return “hisok”.I wanted to use this in CoffeeScript if you want to copy and paste this worked for me
getQueryVariable = (variable) ->
query = window.location.search.substring(1)
tabs is all screwed up
Added another if statement to check for space characters ‘%20’ in the parameters
}
Oops I forgot ‘ != -1 ‘ Sorry about that. Here is the correct code:
}
does anyone have a link to an actual live form where something like this has been implemented that I could look at as an example? Sorry I am not very experienced with javascript and having a lot of trouble figuring out how to set this up. I think I am doing something really basic wrong but I can’t figure out what.
Hey Kat. I made a Codepen implementing the code above.
Hey Chris,
URL Decoding the parameters would be necessary if I’m not mistaken.
Check this solution here: http://stackoverflow.com/a/901144
I am passing a query via the url (http://www.mywebsite.com/appointment-map.aspx?search=33612) and I want to use that query object in the following (urlsearch) and can’t seem to make it work.
In addition to Robert Galindo’s code:
Now we can get more data after the first %20, I mean, all data with spaces, like
uri.html?data=more%20than%20one%20space
I found this info at stackoverflow.com about speeds on “split” and “regex”:
Regards.
Also, as Relfor said, using decodeUriComponent we haven’t to look for spaces (%20) or another symbols (like accents). Less lines of code, better results:
Regards.
Nice Works! God bless my friend
A functional approach (:P):
Even better, don’t do the work once for each query string parameter.
How about combain this function with if/else?
Example:
U have this url
www.domain.com?id=1234&img=test.jpg
Condition
If id=xxxx, then do blablabla, else other.
What script i can use to declare that condition?
Please help. Thanks…
i need YOUR help for check the last 3 char in the URL.
EX:
http://www.google.com
must check last 3 char that is start from dot(.).
EX:
http://www.domainname.xxx
if wrong last 3 char it should move to any alert msg page. Plz help me
can anyone please explain me how to rewrite url using jquery.
Description:
i want rewrite the url by removing the folder path from it.
currently am using asp.net routing which is costly process ,so it would be a great help if any one can give solution for this.
example: products/cloths/jeans.aspx has to be rewritten as jeans.aspx
thanks in advance.
Implemented the original code and id does return the url variable value if the variable exists, else it returns false. The false returned when the variable does not exist is not a string value but, I assume, a javascript boolean? My question is how can I test for the false return, and then take an action? I want to verify that a selection was made that passes a value in the query string, and if not, give the user a message. Thanks for any help.
Hey guys! Great piece of code! Could you tell me how to get current tab url if I am running this code on background page? Any idea if I want to call this code from context menu?