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”);
nice ! thanks.
Great work, this is a really nice bit of coding! As always, keep it up!
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”.thanks to share :D
I tested the function of various types and only this worked.
Congratulations!
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