Skip to content

Instantly share code, notes, and snippets.

@innesm4
Last active August 29, 2015 14:01
Show Gist options
  • Save innesm4/fc1c16bc0bfdb739a9d1 to your computer and use it in GitHub Desktop.
Save innesm4/fc1c16bc0bfdb739a9d1 to your computer and use it in GitHub Desktop.
Function to get URL params of a query string using JavaScript
/*
Function to get a query paramater. In the following URL I need to get the paramater aberdeen to pass to Angular as an
alternative to using $location.search() which wont work in less than ie9.
http://mywebsite.co.uk/search/?location=aberdeen
*/
function geturlParams(value)
{
value = value.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+value+"=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results === null)
return null;
else
return results[1];
}
var location_param = geturlParams('location');
console.log(location_param);
// location_param = aberdeen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment