Skip to content

Instantly share code, notes, and snippets.

@joshnesbitt
Created May 10, 2011 11:45
Show Gist options
  • Select an option

  • Save joshnesbitt/964322 to your computer and use it in GitHub Desktop.

Select an option

Save joshnesbitt/964322 to your computer and use it in GitHub Desktop.
function HostInformation(target, opts){
var opts = opts || {},
output = {},
target = target.location || target;
this.host = function()
{
return target.host;
}
this.fullHost = function()
{
return this.protocol() + "//" + this.host();
}
this.path = function()
{
return target.pathname;
}
this.protocol = function()
{
return target.protocol;
}
this.port = function()
{
return target.port || "8080"
}
this.hash = function()
{
return target.hash.replace('#', '');
}
this.query = function()
{
var q = target.search.replace('?', '');
if(q === '')
return {};
var obj = {},
parts = q.split('&');
for(i in parts){
if(typeof parts[i] === 'string'){
o = parts[i].split("=");
obj[o[0]] = o[1];
}
}
return obj;
}
this.subdomain = function()
{
return this.host().split(".")[0];
}
for(key in this)
if(typeof this[key] === 'function')
output[key] = this[key]();
return output;
}
var info = new HostInformation(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment