Skip to main content Accessibility Feedback

getParams.js

Get the URL parameters from a query string. Learn how it works.

/**
 * Get the URL parameters
 * source: https://css-tricks.com/snippets/javascript/get-url-variables/
 * @param  {String} url The URL
 * @return {Object}     The URL parameters
 */
var getParams = function (url) {
	var params = {};
	var parser = document.createElement('a');
	parser.href = url || window.location.href;
	var query = parser.search.substring(1);
	var vars = query.split('&');
	if (vars.length < 2) return params;
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split('=');
		params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
	}
	return params;
};