freddielore
2/5/2018 - 3:02 PM

Get Query String via JS

Get query string from URL via Javascript #javascript

// e.g URL: https://google.com/?id=foo
// getQueryString('id') // retruns "foo"

getQueryString: function(field, url){
		var href = url ? url : window.location.href;
		var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
		var string = reg.exec(href);
		return string ? string[1] : null;
}