Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  99] [ 5]  / answers: 1 / hits: 34874  / 14 Years ago, tue, december 28, 2010, 12:00:00

Sorry for another simple question, but is there an easy way to read the GET variables from a URL. example. I have a url http://www.domain.com/page.php?var1=1 In my case I will only have 1 variable i.e. var1 or var2 (the variable can change but there will only every be one per url). All the tuts I have seen relate to arrays rather than singletons OK I know an array solution may be better but this is just a simple single get variable. Any suggestions? Thanks in advance


More From » jquery

 Answers
6
var split = location.search.replace('?', '').split('=')


split[0] is your var name, and split[1] is your var value. You actually don't really need jQuery for that piece of code ;)



As for twiz's comment, splitting multiple variables can be done like that:



var split = location.search.replace('?', '').split('&').map(function(val){
return val.split('=');
});


You can access variable name by split[index][0] and value by split[index][1].



Of course you can use the second snippet instead of the first one for one variable too.


[#94467] Saturday, December 25, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackie

Total Points: 442
Total Questions: 107
Total Answers: 94

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
jackie questions
Sat, Sep 18, 21, 00:00, 3 Years ago
Wed, Jul 14, 21, 00:00, 3 Years ago
;