Created
September 2, 2016 09:27
-
-
Save Fabiantjoeaon/ed026d3c47e72a35c8ad9987c1b87e39 to your computer and use it in GitHub Desktop.
ES6 retrieve GET parameters from URL
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
/** | |
* Retrieve parameter values from URL | |
* @param {name} name of paramater | |
* @param {url} URL to fetch parameter from | optional | |
* @returns URL param value || null | |
*/ | |
const getURLParemeters = ( name, url ) => { | |
if (!url) url = location.href; | |
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); | |
var regexS = "[\\?&]"+name+"=([^&#]*)"; | |
var regex = new RegExp( regexS ); | |
var results = regex.exec( url ); | |
return results == null ? null : results[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment