Template:JS/QueryStringUtilities
This page is deprecated. Use the new ParseURL instead.
This template provides four JavaScript functions for working with URL query strings:
- getQueryString() -- grabs the entire query string (everything after the "?") from the current page URL, and returns it unmodified as a String.
- parseQueryString(String queryString) -- accepts a query string (everything after the "?") as input, and parses the parameters into an Object like this:
{"color": "blue", "number": "3", "units": "centimeters"}
. Note that all keys and values in the returned Object will be Strings, so if you want data of a different type, you will have to use conversion methods such as parseInt(). - newURLWithQueryString(String oldURL, Object newQSObject) -- creates a new URL out of a string and an Object of key/value pairs. If the old URL had a query string, it is discarded.
- currentPageButNewQueryString(Object newQSObject) -- is a wrapper around newURLWithQueryString. It uses the current page's URL as oldURL.
These functions do not account for:
- semicolons used as separators
- one key having multiple values
- probably some other edge cases as well
(Also, they may occasionally get confused by MediaWiki's habit of overzealously encoding &
as &
. Keep an eye out for that.)
To use this template, just include {{JS/QueryStringUtilities}} in your page, and freely use the provided functions.