Skip to content

Instantly share code, notes, and snippets.

@loopdream
Last active October 10, 2015 17:40
Show Gist options
  • Save loopdream/1c6b87c4e760b05c138f to your computer and use it in GitHub Desktop.
Save loopdream/1c6b87c4e760b05c138f to your computer and use it in GitHub Desktop.
Url helper
/**
* Url helper functions
* http://jamesallardice.com/urlutils-a-little-set-of-javascript-url-utility-methods/
*/
urlUtils = {
getParam: function(name) {
var regex = new RegExp('[?&]' + name + '=([^&#]*)'),
results = regex.exec(window.location.href);
if(results) {
return results[1];
}
return false;
},
getParams: function() {
var regex = /[?&]([^=#]+)=([^&#]*)/g,
url = window.location.href,
params = {},
match;
while(match = regex.exec(url)) {
params[match[1]] = match[2];
}
return params;
},
getHash: function() {
return window.location.hash.substring(1);
},
makeSlug: function(str) {
return str.toLowerCase().replace(/[^\w ]+/g, '').replace(/ +/g, '-');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment