Skip to content

Instantly share code, notes, and snippets.

@shakesoda
Created June 12, 2013 22:55
Show Gist options
  • Save shakesoda/5769846 to your computer and use it in GitHub Desktop.
Save shakesoda/5769846 to your computer and use it in GitHub Desktop.
snippet for parsing a get variable out of a url
function findVar(attr) {
var url = location.href.split("#")[0];
var search = new RegExp(attr + "=.*?(?=[#&]|$)");
var result = search.exec(url);
if (result !== null) {
result = result[0].split("=")[1];
if (/^-?\d+(\.\d+)?$/.test(result))
return Number(result);
else
return result;
} else return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment