I have seen lots of jQuery examples where parameter size and name are unknown. My url is only going to ever have 1 string:
http://example.com?sent=yes
I just want to detect:
- Does
sent
exist? - Is it equal to "yes"?
I have seen lots of jQuery examples where parameter size and name are unknown. My url is only going to ever have 1 string:
I just want to detect:
|
|||||||||||||||||||||
|
Best solution here.
And this is how you can use this function assuming the URL is,
|
|||||||||||||||||||||
|
jQuery code snippet to get the dynamic variables stored in the url as parameters and store them as JavaScript variables ready for use with your scripts:
example.com?param1=name¶m2=&id=6
example params with spaces
|
|||||||||
|
May be its too late. But this method is very easy and simple
UPDATE |
||||
|
I always stick this as one line. Now params has the vars:
multi-lined:
|
|||||||||||||
|
Or you can use this neat little function, because why overcomplicated solutions?
which looks even better when simplified and onelined: tl;dr one-line solution
result: queryDict['sent'] // undefined or 'value' But what if you have got encoded characters or multivalued keys?You better see this answer: How can I get query string values in JavaScript? Sneak peak
|
|||||
|
Perhaps you might want to give Dentist JS a look? (disclaimer: I wrote the code) code:
with Dentist JS, you can basically call the extract() function on all strings (e.g., document.URL.extract() ) and you get back a HashMap of all parameters found. It's also customizable to deal with delimiters and all. Minified version < 1kb |
||||
|
There's this great library: https://github.com/allmarkedup/purl which allows you to do simply
The example is assuming you're using jQuery. You could also use it just as plain javascript, the syntax would then be a little different. |
|||||
|
Try this working demo http://jsfiddle.net/xy7cX/ API:
This should help code
|
|||||
|
I hope this will help.
|
||||
|
This will give you a nice object to work with
And then;
|
||||
|
This might be overkill, but there is a pretty popular library now available for parsing URIs, called URI.js. Example
|
|||
|
|
|||||||||
|
Coffeescript version of Sameer's answer
|
|||
|
use this
|
|||
|
A slight improvement to Sameer's answer, cache params into closure to avoid parsing and looping through all parameters each time calling
|
|||
|
I use this and it works. http://codesheet.org/codesheet/NF246Tzs
|
|||
|