How to get Uri with Javascript (ParseUri)

This should be useful to get the url uri... it will become an object so just add youvar.path or something, if you console.log the variable it should divide into 10 variable that you can choose to display.


function parseUri(sourceUri){
var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(d*))?)?((/(?:[^?#](?![^?#/]*.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))?(?:?([^#]*))?(?:#(.*))?").exec(sourceUri);
var uri = {};

for(var i = 0; i < 10; i++){
uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
}
return uri;
}

Subscribe to You Live What You Learn

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe