Skip to content

Instantly share code, notes, and snippets.

@julianfrank
Created August 30, 2016 07:40
Show Gist options
  • Save julianfrank/7d93183531cea35c696cd413a1ffdb5a to your computer and use it in GitHub Desktop.
Save julianfrank/7d93183531cea35c696cd413a1ffdb5a to your computer and use it in GitHub Desktop.
Parse URL from Client JS
/*While window.location.search can be useful I'd much rather the search property be a key: value object.
An object would be nice but as long as we can get one property at a time, that would suffice.
The following is a function stolen from the A-Frame VR toolkit:*/
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
/*With the function above, you can get individual parameter values:*/
getUrlParameter('post'); // "1234"
getUrlParameter('action'); // "edit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment