Created
August 30, 2016 07:40
-
-
Save julianfrank/7d93183531cea35c696cd413a1ffdb5a to your computer and use it in GitHub Desktop.
Parse URL from Client JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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