Skip to content

Instantly share code, notes, and snippets.

@ded
Created October 15, 2010 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ded/627367 to your computer and use it in GitHub Desktop.
Save ded/627367 to your computer and use it in GitHub Desktop.
Mimics the window.location parts when given a URL
twttr.klass('twttr.util.Location', function(url) {
if (url.match(/^\/\//)) {
url = 'http:' + url;
} else if (!url.match(/^[a-z]+:\/\//)) {
url = 'http://' + url;
}
var m = url.match(/^([a-z]+:)\/\/([\w\-\.]+)(\:\d+)?(.+)?/);
this.href = url;
this.protocol = m[1];
this.hostname = m[2];
this.host = m[2] + (m[3] || '');
this.port = m[3] ? m[3].slice(1) : '80';
this.extension = m[2].match(/\.(\w+)$/)[1];
if (!m[4]) {
this.hash = '';
this.search = '';
this.pathname = '/';
} else {
if (!m[4].match(/^\//)) {
m[4] = '/' + m[4];
}
var query = m[4].split('?');
var hash = m[4].split('#');
this.pathname = query[0];
if (!((/\/$/).test(this.pathname))) {
this.pathname = this.pathname + '/';
}
this.search = query.length > 1 ? '?' + (function() {
query.shift();
return query.join('').replace(/#.+$/, '');
}()) : '';
this.hash = hash.length > 1 ? '#' + hash[1] : '';
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment