/*
* uriToJSON
* URI to JSON Serializer JavaScript Class
* developer's website: http://wemakesites.net/#!/web-dev
* developer's twitter: https://twitter.com/#!/wemakesitesnet
* developer's blog http://acidmartin.wordpress.com/
**/
function uriToJSON(uri) {
"use strict";
if(!("hostname" in document.createElement("a"))) {
return;
}
var
anchor = document.createElement("a"),
params = {},
data,
param;
anchor.setAttribute("href", uri);
data = {
hash: anchor.hash.replace("#", ""),
host: anchor.host,
hostname: anchor.hostname,
parameters: null,
pathname: anchor.pathname,
port: parseInt(anchor.port) ? parseInt(anchor.port) : anchor.port,
protocol: anchor.protocol,
uri: uri
};
if(anchor.search.split("?")[1]) {
params = anchor.search.split("?")[1].split("&");
data.parameters = {};
}
for(param in params) {
if(params.hasOwnProperty) {
var
pair = params[param].split("="),
key = pair[0],
value = pair[1] === "true" ? true : pair[1] === "false" ? false : pair[1];
data.parameters[key] = parseInt(value) ? parseInt(value) : value;
}
}
return data;
}