Some JavaScript + REGEX goo to get query strings from your HREF/URI/URL - DevCommunity.info


Some JavaScript + REGEX goo to get query strings from your HREF/URI/URL

Posted by Adron (compositecode.com) on August 6, 2011

I guess there is a library somewhere to do this, but I couldn't find it fast enough so I spit this contraption out. It's a function that grabs the value from a URI/URL query string. Yes, writing those nasty REGEXs about drove me nuts! ;) Maybe I can save at least one soul out there some trouble. If

... [read more]

Rating

4/5

Reviews

This is a solution using REGEX to retrieve query string parameters from the current url.

Performance may be an issue using the method if called multiple times, as noted in comments to the post. A link to an alternative solution on stackoverflow is also provided.

The following series of posts provide another set of JavaScript functions that can be used to test / get and set query string parameters for the current url. Haven't tested these for performance, but they don't use REGEX so would differ from the solution above. JavaScript redirects are used to apply changes to the query string for the current page.

Add Query String Parameter
Adds a query string parameter to the current url (updates the value if it already exists).

Check if Query String Parameter Exists in current URL
Test if a specific parameter exists in the current url.

Remove a parameter from the query string if found in current url
Remove a specific parameter and value from the current url.

Get the value of the specified Query String Parameter
Returns the value of the specified parameter if found.

Get all parameters and values as an Array
Returns an array of parameters and values from the current url.

Remove all query string parameters from the current url
Strip all query string parameters from the current url.

Add query string parameter dynamically to some or all links on the current page
This one I found quite useful, as it dynamically updates hyperlinks on the current page to include the specified parameter and value. Performance doesn't seem to be an issue, but would be affected by many things particularly seeing that the script is client-side. This may be useful when you want to include a parameter in links instead of adding it after vising one of the pages/links.
By GeorgeH on August 31, 2011 9:32PM

Submit a review:

Login required.