Skip to content

Instantly share code, notes, and snippets.

@jadjoubran
Forked from aymanfarhat/urlobject.js
Last active December 17, 2015 13:09
Show Gist options
  • Save jadjoubran/5614791 to your computer and use it in GitHub Desktop.
Save jadjoubran/5614791 to your computer and use it in GitHub Desktop.
Instead of returning #hash the function will now return 'hash' I don't think that there is any case where the # in the urlObject.hash will be useful
function urlObject(options)
{
default_options = {'url':window.location.href,'unescape':true,'convert_num':true};
if(typeof options !== "object")
options = default_options;
else
{
for(var index in default_options)
{
if(typeof options[index] ==="undefined")
options[index] = default_options[index];
}
}
var a = document.createElement('a');
a.href = options['url'];
url_query = a.search.substring(1);
var params = {};
var vars = url_query.split('&');
if(vars[0].length > 1)
{
for(var i = 0; i < vars.length; i++)
{
var pair = vars[i].split("=");
var key = (options['unescape'])?unescape(pair[0]):pair[0];
var val = (options['unescape'])?unescape(pair[1]):pair[1];
if(options['convert_num'])
{
if(val.match(/^\d+$/))
val = parseInt(val);
else if(val.match(/^\d+\.\d+$/))
val = parseFloat(val);
}
if(typeof params[key] === "undefined")
params[key] = val;
else if(typeof params[key] === "string")
params[key] = [params[key],val];
else
params[key].push(val);
}
}
var urlObj = {
protocol:a.protocol,
hostname:a.hostname,
host:a.host,
port:a.port,
hash:a.hash.substr(1),
pathname:a.pathname,
search:a.search,
parameters:params
};
return urlObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment