get url parameters

function get(key) {
	var searchParams = {},
		search = window.location.search.substring(1); // substring to remove the ?

	if (search) {
		search = search.split('&');
		for (var i in search) {
			if (search.hasOwnProperty(i) && i !== '') {
				var pair = search[i].split('=');
				pair[0] = decodeURIComponent(pair[0]);
				pair[1] = decodeURIComponent(pair[1]);
				searchParams[pair[0]] = pair[1];
			}
		}
	}

	if (key) {
		return searchParams[key];
	}

	return searchParams;
}

Leave a Reply

Your email address will not be published. Required fields are marked *