Created
October 15, 2010 00:41
-
-
Save ded/627367 to your computer and use it in GitHub Desktop.
Mimics the window.location parts when given a URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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