Difference between revisions of "Template:JS/QueryStringUtilities"

(copied from igem.org)
 
(rolled in changes from igem.org)
Line 30: Line 30:
 
 
 
return ret;
 
return ret;
 +
}
 +
 +
function newURLWithQueryString(oldURL, newQSObject) {
 +
var urlWithoutQS = oldURL.split("?")[0];
 +
return urlWithoutQS + "?" + jQuery.param(newQSObject);
 +
}
 +
 +
function currentPageButNewQueryString(newQSObject) {
 +
return newURLWithQueryString(document.location.href, newQSObject);
 
}
 
}
  
Line 37: Line 46:
  
 
<noinclude>
 
<noinclude>
This template provides JavaScript functions getQueryString() and parseQueryString(qs).
+
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.
+
* '''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: <code>{"color": "blue", "number": "3", "units": "centimeters"}</code>. 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.
  
parseQueryString(qs) accepts a query string as input, and parses the parameters into an Object like this: <code>{"color": "blue", "number": "3", "units": "centimeters"}</code>. Note that everything in the returned Object will be a String, so if you want to get data of a different type, you will have to use conversion methods such as parseInt().
+
 
+
These functions do not account for:
The methods in this template do not account for:
+
 
* semicolons used as separators
 
* semicolons used as separators
 
* one key having multiple values
 
* one key having multiple values
* probably some other edge cases as well :(
+
* probably some other edge cases as well
 +
 
 +
 
 +
(Also, they may occasionally get confused by MediaWiki's habit of overzealously encoding <code>&</code> as <code>&amp;amp;</code>. Keep an eye out for that.)
 +
 
 +
 
 +
To use this template, just include <nowiki>{{JS/QueryStringUtilities}}</nowiki> in your page, and freely use the provided functions.
 
</noinclude>
 
</noinclude>

Revision as of 18:17, 24 February 2015


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 &amp;. Keep an eye out for that.)


To use this template, just include {{JS/QueryStringUtilities}} in your page, and freely use the provided functions.