Last active
August 29, 2015 14:01
-
-
Save innesm4/fc1c16bc0bfdb739a9d1 to your computer and use it in GitHub Desktop.
Function to get URL params of a query string using JavaScript
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
| /* | |
| 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