Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
304 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...