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
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
With ES2019 to create an object:
The Object.fromEntries method creates an object from an array.
For the URL
http://www.example.com/page?var1=1&var2=2
the above code returns the objectsplit[0]
is your var name, andsplit[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:
You can access variable name by
split[index][0]
and value bysplit[index][1]
.Of course you can use the second snippet instead of the first one for one variable too.
You can use this function that returns the value of the specified var name from url if the var exists. Otherwise you will get an empty string.
I use this in my default javascript file.
Now you can use them by name: