public
Last active

JavaScript + DOM implementation of http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html

  • Download Gist
test-url.html
HTML
1 2 3 4 5 6 7 8 9 10 11
<!DOCTYPE html>
<script src="url.js"></script>
<script>
 
var url = new URL('http://www.example.com/a/b/c.html?p=q&r=s&p&p=t#hash');
 
for (var key in url) {
console.log(key, url[key]);
}
 
</script>
url.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
 
var URL = (function() {
 
function parseSearch(s) {
var result = [];
var k = 0;
var parts = s.slice(1).split('&');
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
var key = part.split('=', 1)[0];
if (key) {
var value = part.slice(key.length + 1);
result[k++] = [key, value];
}
}
return result;
}
 
function serializeParsed(array) {
return array.map(function(pair) {
return pair[1] !== '' ? pair.join('=') : pair[0];
}).join('&');
}
 
function URL(url, base) {
if (!url)
throw new TypeError('Invalid argument');
 
var doc = document.implementation.createHTMLDocument('');
if (base) {
var baseElement = doc.createElement('base');
baseElement.href = base || window.lo;
doc.head.appendChild(baseElement);
}
var anchorElement = doc.createElement('a');
anchorElement.href = url;
doc.body.appendChild(anchorElement);
 
if (anchorElement.href === '')
throw new TypeError('Invalid URL');
 
Object.defineProperty(this, '_anchorElement', {value: anchorElement});
}
 
URL.prototype = {
toString: function() {
return this.href;
},
get href() {
return this._anchorElement.href;
},
set href(value) {
this._anchorElement.href = value;
},
 
get protocol() {
return this._anchorElement.protocol;
},
set protocol(value) {
this._anchorElement.protocol = value;
},
 
// NOT IMPLEMENTED
// get username() {
// return this._anchorElement.username;
// },
// set username(value) {
// this._anchorElement.username = value;
// },
 
// get password() {
// return this._anchorElement.password;
// },
// set password(value) {
// this._anchorElement.password = value;
// },
 
// get origin() {
// return this._anchorElement.origin;
// },
 
get host() {
return this._anchorElement.host;
},
set host(value) {
this._anchorElement.host = value;
},
 
get hostname() {
return this._anchorElement.hostname;
},
set hostname(value) {
this._anchorElement.hostname = value;
},
 
get port() {
return this._anchorElement.port;
},
set port(value) {
this._anchorElement.port = value;
},
 
get pathname() {
return this._anchorElement.pathname;
},
set pathname(value) {
this._anchorElement.pathname = value;
},
 
get search() {
return this._anchorElement.search;
},
set search(value) {
this._anchorElement.search = value;
},
 
get hash() {
return this._anchorElement.hash;
},
set hash(value) {
this._anchorElement.hash = value;
},
 
get filename() {
var match;
if ((match = this.pathname.match(/\/([^\/]+)$/)))
return match[1];
return '';
},
set filename(value) {
var match, pathname = this.pathname;
if ((match = pathname.match(/\/([^\/]+)$/)))
this.pathname = pathname.slice(0, -match[1].length) + value;
else
this.pathname = value;
},
 
get parameterNames() {
var seen = Object.create(null);
return parseSearch(this.search).map(function(pair) {
return pair[0];
}).filter(function(key) {
if (key in seen)
return false;
seen[key] = true;
return true;
});
},
 
getParameter: function(name) {
return this.getParameterAll(name).pop();
},
 
getParameterAll: function(name) {
name = String(name);
var result = [];
var k = 0;
parseSearch(this.search).forEach(function(pair) {
if (pair[0] === name)
result[k++] = pair[1];
});
return result;
},
 
appendParameter: function(name, values) {
if (!Array.isArray(values))
values = [values];
var parsed = parseSearch(this.search);
for (var i = 0; i < values.length; i++) {
parsed.push([name, values[i]]);
}
this.search = serializeParsed(parsed);
},
 
clearParameter: function(name) {
this.search = serializeParsed(
parseSearch(this.search).filter(function(pair) {
return pair[0] !== name;
}));
},
};
 
var oldURL = window.URL || window.webkitURL || window.mozURL;
 
URL.createObjectURL = function(blob) {
return oldURL.createObjectURL.apply(oldURL, arguments);
};
 
URL.revokeObjectURL = function(url) {
return oldURL.revokeObjectURL.apply(oldURL, arguments);
};
 
// Methods should not be enumerable.
Object.defineProperty(URL.prototype, 'toString', {enumerable: false});
Object.defineProperty(URL.prototype, 'getParameter', {enumerable: false});
Object.defineProperty(URL.prototype, 'getParameterAll', {enumerable: false});
Object.defineProperty(URL.prototype, 'appendParameter', {enumerable: false});
Object.defineProperty(URL.prototype, 'clearParameter', {enumerable: false});
Object.defineProperty(URL, 'createObjectURL', {enumerable: false});
Object.defineProperty(URL, 'revokeObjectURL', {enumerable: false});
 
return URL;
})();

I think you need to use window.URL for modern Firefox. window.mozURL is undefined in Firefox Nightly 11.

Done. Thanks.

On Mon, Nov 21, 2011 at 16:27, Sam Tobin-Hochstadt
reply@reply.github.com
wrote:

I think you need to use window.URL for modern Firefox.  window.mozURL is undefined in Firefox Nightly 11.


Reply to this email directly or view it on GitHub:
https://gist.github.com/1384398

erik

Hello, guys!
i have this code https://gist.github.com/1235332 for URL resolving, may be it's better to include this and not use DOM ?

Without DOM this script will work under node.js and WebWorkers and performnace will be increased.

This gist was created in response to https://bugs.webkit.org/show_bug.cgi?id=71968 which is about adding a URL host object to WebKit.

Implementing a correct URL object in JS requires thousands of lines of code that needs to be downloaded over and over. All browsers already ship with C++ implementations of URLs already so we want to leverage this.

@arv, the algorithm is described at the spec. - https://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#concept-url-parser
do not think it requires thousands of lines of code

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.