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
|
|||||||||||||
|
I always stick this as one line. Now params has the vars:
multi-lined:
|
|||||||||||||||||||||
|
May be its too late. But this method is very easy and simple
UPDATE |
|||||
|
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;
|
||||
|
Coffeescript version of Sameer's answer
|
|||
|
This might be overkill, but there is a pretty popular library now available for parsing URIs, called URI.js. Example
|
|||
|
|
|||||||||
|
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
|
|||
|
With vanilla JavaScript, you could easily take the params (location.search), get the substring (without the ?) and turn it into an array, by splitting it by '&'. As you iterate through urlParams, you could then split the string again with '=' and add it to the 'params' object as object[elmement[0]] = element[1]. Super simple and easy to access. http://www.website.com/?error=userError&type=handwritten
|
|||
|
What if there is & in URL parameter like filename="p&g.html"&uid=66 In this case the 1st function will not work properly. So I modified the code
|
||||
|
This is based on Gazoris's answer, but URL decodes the parameters so they can be used when they contain data other than numbers and letters:
|
|||
|
Admittedly I'm adding my answer to an over-answered question, but this has the advantages of: -- Not depending on any outside libraries, including jQuery -- Not polluting global function namespace, by extending 'String' -- Not creating any global data and doing unnecessary processing after match found -- Handling encoding issues, and accepting (assuming) non-encoded parameter name -- Avoiding explicit
Then you'd use it as:
|
|||
|
So simple you can use any url and get value
Usage Example
Note: If a parameter is present several times (?first=value1&second=value2), you will get the first value (value1) and second value as (value2). |
|||
|