Timeline for How to get URL parameter using jQuery or plain JavaScript?
Current License: CC BY-SA 4.0
58 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jan 24, 2024 at 23:33 | review | Suggested edits | |||
| Jan 26, 2024 at 18:37 | |||||
| Apr 14, 2023 at 3:49 | comment | added | Peter Thoeny | The + character represents a space, and is not handled by decodeURIComponent(). To support that specify: decodeURIComponent(sParameterName[1]).replace(/\+/g, ' ') | |
| Jul 31, 2022 at 16:14 | history | edited | Derek Adair | CC BY-SA 4.0 | deleted 5 characters in body |
| Jul 19, 2022 at 8:59 | review | Suggested edits | |||
| Jul 20, 2022 at 11:13 | |||||
| Jun 23, 2022 at 15:54 | comment | added | Stefan | 'here' link at the beginning of post is broken. | |
| Dec 2, 2021 at 11:15 | history | edited | Andrew Bennet | CC BY-SA 4.0 | Fix error: code was comparing a variable type with undefined, rather than the value |
| Feb 4, 2021 at 8:58 | history | edited | user753676 | CC BY-SA 4.0 | added 8 characters in body |
| Oct 23, 2019 at 23:52 | review | Suggested edits | |||
| Oct 24, 2019 at 3:57 | |||||
| Mar 31, 2019 at 9:05 | comment | added | ikegami | sParameterName[0] should be decodeURIComponent(sParameterName[0]) | |
| Mar 22, 2019 at 21:11 | review | Suggested edits | |||
| Mar 23, 2019 at 2:30 | |||||
| Feb 25, 2019 at 15:25 | review | Suggested edits | |||
| Feb 25, 2019 at 17:06 | |||||
| Dec 8, 2018 at 12:10 | comment | added | Abdallah Arffak | in my opinion, the code will be perfect if you change the IF condition to be : ` if (decodeURIComponent(sParameterName[0]) === sParam || sParameterName[0] === sParam) ` so that if we compare the variables that are already encoded as the arrays the condition "IF" will pass | |
| Nov 3, 2018 at 19:51 | history | edited | Sebastian S | CC BY-SA 4.0 | Fixed code. It is working _incorrectly_ as noted in the comments by Ivan Skalauh and xzoert. |
| Sep 19, 2018 at 16:05 | comment | added | nicolallias | Fix @Nadu comment: var url = new URL( 'http://example.com?sent=yes' ), sendval = url. searchParams.get('sent') see developer.mozilla.org/en-US/docs/Web/API/… | |
| Sep 7, 2018 at 13:18 | comment | added | Penguin9 | Why do you name the function twice? Just delete the var getUrlParameter = if you want to keep the function name | |
| S May 30, 2018 at 5:04 | history | rollback | Mujammil H Kazi | Rollback to Revision 8 - Edit approval overridden by post owner or moderator | |
| May 30, 2018 at 3:00 | history | suggested | Jürg | CC BY-SA 4.0 | the name is set twice |
| May 29, 2018 at 19:16 | review | Suggested edits | |||
| S May 30, 2018 at 5:04 | |||||
| May 5, 2018 at 5:45 | comment | added | user734028 | I am missing something, what part of it is jquery, isnt this plain javascript? | |
| Mar 3, 2018 at 18:34 | comment | added | Nadu | var url = new URI ( 'example.com?sent=yes' ); var sendval = url. searchParams ` (` 'sent' );` and you're done! I down voted this. Even if it's nice tempting and a good coding practice to write ya one homebrew function. Primary make use of functions ya library / framework offers, until you've good reason to do not. | |
| Feb 22, 2018 at 17:23 | comment | added | Paul Allsopp | It works but is very inefficient. You're performing all the same work every time. Create an object that has a "params" list and that function as a method. When you call the function, populate the list of params. Then have a getter which returns items from the list. If you're going to write a function that does the same thing each time, use RegEx, far faster, far more efficient | |
| Jun 23, 2017 at 23:12 | review | Suggested edits | |||
| Jun 24, 2017 at 9:27 | |||||
| Mar 27, 2017 at 22:21 | comment | added | Florin Andrei | you should use var window.location['pathname'] instead of window.location.search.substring(1) or else this will not work. | |
| Mar 11, 2017 at 19:55 | comment | added | Ivan Skalauh | It's actually wrong; you should urldecode parameters AFTER you have split the string, not before. Otherwise it will freak out if '&' or '=' are encoded there in the actual parameters. | |
| Feb 2, 2017 at 17:22 | comment | added | Stanislav Potapenko | I have some problem with this script so I have found better here stackoverflow.com/questions/901115/… | |
| Jan 25, 2017 at 11:05 | comment | added | xzoert | I think you shoud call decodeURIComponent on sParameterName[0] before comparing and on sParameterName[1] before returning, not on the whole string... What happens if a parameter name or value has an (encoded) '&' or '=' in it? You will do the wrong split! | |
| Sep 25, 2016 at 17:23 | review | Suggested edits | |||
| Sep 26, 2016 at 0:17 | |||||
| Jul 13, 2016 at 23:26 | comment | added | Master DJon | I added return false; at the end of the function. | |
| Mar 9, 2016 at 4:22 | history | rollback | Mujammil H Kazi | Rollback to Revision 6 | |
| S Mar 8, 2016 at 14:23 | history | suggested | Sam Texas | CC BY-SA 3.0 | removed redundant function name from code |
| Mar 8, 2016 at 13:41 | review | Suggested edits | |||
| S Mar 8, 2016 at 14:23 | |||||
| Feb 13, 2016 at 20:05 | comment | added | alexw | This doesn't seem to work for multivalued keys (like ?technology=jquery&technology=css). | |
| Feb 8, 2016 at 9:30 | comment | added | Yulian | Since query string parameters are case insensitive, I think it would be better if when looking for a match with the parameter name provided the strings that are compared should be transformed to lower- or uppercase, e.g. if (sParameterName[0].toLowerCase() === sParam.toLowerCase()) | |
| Jan 9, 2016 at 22:28 | comment | added | Rufus | I needed to get a certain param from api generated data that was unpredictably separated sometimes by a question mark and sometimes an ampersand. Replacing the third line of Kazi's code with this regex: sURLVariables = sPageURL.split(new RegExp('[\?&]')) works for me in that situation. | |
| Dec 2, 2015 at 13:46 | comment | added | zakinster | The decoding should be done on the parameter value (as Iouri suggests). If the decoding is done on the url (as in the answer), it won't work correctly if there's an encoded & or = in a parameter value. | |
| Oct 13, 2015 at 20:40 | comment | added | Lyubomir Velchev | you can return empty string explicitly if no matches found - just before the last line of code. return ''; }; In that way all the code will return value | |
| Jul 28, 2015 at 6:37 | comment | added | Rob Evans | I've updated the answer to include all the comments code changes above this comment. | |
| Jul 27, 2015 at 15:43 | history | edited | Rob Evans | CC BY-SA 3.0 | Moved variable delcaration to single call, added decodeURIComponent to handle special characters, added code to handle value-less parameters, added local declaration when used inside scope of another closure to avoid globals. |
| May 21, 2015 at 9:12 | comment | added | Remigius Stalder | Just to mention it - this script fragment also works without jQuery. | |
| May 10, 2015 at 6:52 | review | Suggested edits | |||
| May 10, 2015 at 8:05 | |||||
| May 10, 2015 at 5:56 | comment | added | elsadek | need to add sPageURL=decodeURIComponent(sPageURL); to have special chars like space and " displayed correctly | |
| May 6, 2015 at 16:14 | comment | added | chad | I would use "=== sParam" vs "== sParam" otherwise sParam = 'true' would match everything. Besides, always using === is a good idea period. | |
| May 1, 2015 at 15:28 | comment | added | sebt | To handle valueless params like ?param1¶m2 modify the return to: return sParameterName[1] === undefined ? true : sParameterName[1]; | |
| Apr 16, 2015 at 19:01 | comment | added | Cody Moniz | As per stackoverflow.com/questions/8486099/…, location.search won't work for hash based routing (#) | |
| Mar 2, 2015 at 14:52 | review | Suggested edits | |||
| Mar 2, 2015 at 15:20 | |||||
| Feb 11, 2015 at 13:44 | comment | added | Christophe | This solution works pretty well for me I've just used var sPageURL = decodeURI(window.location.search.substring(1)); to convert %20 characters into white spaces and also I return an empty string instead of nothing if the parameter is not matched. | |
| Feb 9, 2015 at 14:06 | comment | added | Stefano Caravana | Return a false or null for empty search | |
| Dec 5, 2014 at 19:58 | comment | added | smoak | Be careful, ; is a legal delimeter which this doesn't account for. | |
| Nov 11, 2014 at 6:42 | comment | added | albusshin | How to convert the "+" inside parameter value into whitespaces? | |
| S Sep 2, 2014 at 18:33 | history | edited | ohmu | CC BY-SA 3.0 | Removed the invisible syntax error caused by "zero-width whitespace (\u200b) towards the end there", observed by @Christofer Olsson |
| S Sep 2, 2014 at 18:33 | history | suggested | jaraics | CC BY-SA 3.0 | Removed the invisible syntax error caused by "zero-width whitespace (\u200b) towards the end there", observed by @Christofer Olsson |
| Sep 2, 2014 at 18:09 | review | Suggested edits | |||
| S Sep 2, 2014 at 18:33 | |||||
| Aug 12, 2014 at 8:54 | comment | added | Christofer Ohlsson | Thanks! But when copying this, I found a nasty surprise, involving a zero-width whitespace (\u200b) towards the end there. Making the script have an invisible syntax error. | |
| Jul 31, 2014 at 11:31 | vote | accept | LeBlaireau | ||
| Apr 4, 2014 at 20:59 | history | edited | Will | CC BY-SA 3.0 | changed to follow JavaScript convention |
| S Mar 18, 2014 at 19:20 | history | suggested | Kevin | CC BY-SA 3.0 | Added a link to the source website where this answer seems to be cited from. |
| Mar 18, 2014 at 19:19 | review | Suggested edits | |||
| S Mar 18, 2014 at 19:20 | |||||
| Feb 20, 2014 at 9:12 | history | answered | Mujammil H Kazi | CC BY-SA 3.0 |