forked from medialize/URI.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
docs.html
366 lines (313 loc) · 16.2 KB
/
docs.html
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>URI.js - API Documentation</title>
<meta name="description" content="Mutation facility for URLs." />
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="prettify/prettify.js" type="text/javascript"></script>
<script src="screen.js" type="text/javascript"></script>
<link href="screen.css" rel="stylesheet" type="text/css" />
<link href="prettify/prettify.sunburst.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-8922143-3']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<a id="github-forkme" href="https://github.com/medialize/URI.js"><img src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
<div id="container">
<h1><a href="https://github.com/medialize/URI.js">URI.js</a></h1>
<ul class="menu">
<li><a href="index.html">About</a></li>
<li class="active"><a href="docs.html">Documentation</a></li>
<li><a href="http://rodneyrehm.de/en/">Author</a></li>
</ul>
<ul class="toc">
<li><a href="#constructor">Constructing an URI</a></li>
<li>
Working with URI parts
<ul>
<li><a href="#accessors-protocol">protocol()</a></li>
<li><a href="#accessors-username">username()</a></li>
<li><a href="#accessors-password">password()</a></li>
<li><a href="#accessors-hostname">hostname()</a></li>
<li><a href="#accessors-port">port()</a></li>
<li><a href="#accessors-host">host()</a></li>
<li><a href="#accessors-authority">authority()</a></li>
<li><a href="#accessors-domain">domain()</a></li>
<li><a href="#accessors-tld">tld()</a></li>
<li><a href="#accessors-pathname">pathname(), path()</a></li>
<li><a href="#accessors-directory">directory()</a></li>
<li><a href="#accessors-filename">filename()</a></li>
<li><a href="#accessors-suffix">suffix()</a></li>
<li><a href="#accessors-search">search(), query()</a></li>
<li><a href="#accessors-hash">hash(), fragment()</a></li>
</ul>
</li>
<li>
Working with the query string
<ul>
<li><a href="#accessors-search">search(), query()</a></li>
<li><a href="#search-add">addSearch(), addQuery()</a></li>
<li><a href="#search-remove">removeSearch(), removeQuery()</a></li>
</ul>
</li>
<li>
Normalizing an URL
<ul>
<li><a href="#normalize">normalize()</a></li>
<li><a href="#normalize-host">normalizeHostname()</a></li>
<li><a href="#normalize-port">normalizePort()</a></li>
<li><a href="#normalize-path">normalizePathname(), normalizePath()</a></li>
<li><a href="#normalize-search">normalizeSearch(), normalizeQuery()</a></li>
<li><a href="#normalize-hash">normalizeHash(), normalizeFragment()</a></li>
</ul>
</li>
<li>
Relative and Absolute URLs
<ul>
<li><a href="#relativeto">relativeTo()</a></li>
<li><a href="#absoluteto">absoluteTo()</a></li>
</ul>
</li>
</ul>
<h2 id="api-methods">URI.js API</h2>
<h3 id="constructor">URI Constructor</h3>
<pre class="prettyprint lang-js">var uri = new URI(); // same as new URI(location.href)
// string
var uri = new URI("http://example.org");
// URI object for cloning
var uri = new URI(new URI("http://example.org"));
// URI parts object
var uri = new URI({
protocol: 'http',
host: 'example.org'
});
// without new keyword
var uri = URI("example.org");</pre>
<p>The following parts can be specified in an object:</p>
<pre class="prettyprint lang-js">var uri = new URI({
protocol: 'http', // no trailing :
username: 'user',
password: 'pass',
hostname: 'example.org',
port: '80', // string, please
// "path", not "pathname", sorry
path: '/foo/bar.html',
// "query", not "search", sorry
query: 'foo=bar&bar=baz', // no leading ?
// "fragment", not "hash", sorry
fragment: 'frag' // no leading #
});</pre>
<h3 id="accessors-protocol">protocol()</h3>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
// get protocol
uri.protocol(); // returns string "http"
// set protocol
uri.protocol("ftp"); // returns the URI instance for chaining</pre>
<h3 id="accessors-username">username()</h3>
<pre class="prettyprint lang-js">var uri = new URI("http://user:[email protected]/foo/hello.html");
// get username
uri.username(); // returns string "user"
// set username
uri.username("user"); // returns the URI instance for chaining</pre>
<h3 id="accessors-password">password()</h3>
<pre class="prettyprint lang-js">var uri = new URI("http://user:[email protected]/foo/hello.html");
// get password
uri.password(); // returns string "pass"
// set password
uri.password("user"); // returns the URI instance for chaining</pre>
<h3 id="accessors-hostname">hostname()</h3>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
// get protocol
uri.hostname(); // returns string
// set protocol
uri.hostname("example.org"); // returns the URI instance for chaining</pre>
<p>NOTE: <a href="#accessors-hostname">.hostname()</a> returns the actual hostname, whereas <a href="#accessors-host">.host()</a> returns the hostname including the port</p>
<h3 id="accessors-port">port()</h3>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org:8080/foo/hello.html");
// get port
uri.port(); // returns string "8080"
// set port
uri.port("80"); // returns the URI instance for chaining</pre>
<p>NOTE: although the port may be considered an integer, within URI it is a string.</p>
<h3 id="accessors-host">host()</h3>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org:80/foo/hello.html");
// get host
uri.host(); // returns string "example.org:80"
// set host
uri.host("example.org:80"); // returns the URI instance for chaining</pre>
<p>NOTE: <a href="#accessors-hostname">.hostname()</a> returns the actual hostname, whereas <a href="#accessors-host">.host()</a> returns the hostname including the port</p>
<h3 id="accessors-authority">authority()</h3>
<p>Authority is comprised of username, password, hostname and port</p>
<pre class="prettyprint lang-js">var uri = new URI("http://user:[email protected]:88/foo/hello.html");
// get authority
uri.authority(); // returns string "user:[email protected]:88"
// set authority
uri.authority("user:[email protected]:80"); // returns the URI instance for chaining</pre>
<p>NOTE: .authority() will reset any of username, password and port if they're not specified.</p>
<h3 id="accessors-authority">domain()</h3>
<p>.domain() is a convenience method that returns <code>example.org</code> from the hostname <code>www.example.org</code>.</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
// get domain
uri.domain(); // returns string "example.org"
// set domain
uri.domain("otherdomain.com"); // returns the URI instance for chaining</pre>
<p>NOTE: .domain() will throw an error if you pass it an empty string.</p>
<h3 id="accessors-tld">tld()</h3>
<p>.tld() is a convenience method that returns <code>org</code> from the hostname <code>www.example.org</code>.</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
// get tld
uri.tld(); // returns string "org"
// set tld
uri.tld("com"); // returns the URI instance for chaining</pre>
<p>NOTE: .tld() will throw an error if you pass it an empty string or use it on an IP-host.</p>
<h3 id="accessors-pathname">pathname(), path()</h3>
<p>.path() is an alias of .pathname()</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
// get pathname
uri.pathname(); // returns string "/foo/hello.html"
// set pathname
uri.pathname("/foo/hello.html"); // returns the URI instance for chaining</pre>
<h3 id="accessors-directory">directory()</h3>
<p>.directory() is an convenience method for mutating the directory part of a path</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
// get directory
uri.directory(); // returns string "/foo" (no trailing slash)
// set directory
uri.directory("/bar"); // returns the URI instance for chaining
// uri == "http://example.org/bar/hello.html"</pre>
<h3 id="accessors-filename">filename()</h3>
<p>.filename() is an convenience method for mutating the filename part of a path</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
// get filename
uri.filename(); // returns string "hello.html" (no leading slash)
// set filename
uri.filename("world.xml"); // returns the URI instance for chaining
// uri == "http://example.org/bar/world.xml"</pre>
<h3 id="accessors-suffix">suffix()</h3>
<p>.suffix() is an convenience method for mutating the filename part of a path</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html");
// get suffix
uri.suffix(); // returns string "html" (no leading dot)
// set suffix
uri.suffix("xml"); // returns the URI instance for chaining
// uri == "http://example.org/bar/world.xml"</pre>
<h3 id="accessors-search">search(), query()</h3>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html?foo=bar&bar=baz");
// get search
uri.search(); // returns string "?foo=bar&bar=baz" (leading ?)
// get query
uri.query(); // returns string "foo=bar&bar=baz" (no leading ?)
// .query() and .search() behave the same for the following:
// set search
uri.search("?foo=bar&bar=baz"); // returns the URI instance for chaining
uri.search("foo=bar&bar=baz"); // returns the URI instance for chaining
uri.search(""); // returns the URI instance for chaining
// uri == "http://example.org/bar/world.html"
// get data map:
uri.search(true); // returns { foo: "bar", hello : ["world", "mars"] }
// set data map:
uri.search({ foo: "bar", hello : ["world", "mars"] });
// uri == "http://example.org/bar/world.html?foo=bar&hello=world&hello=mars"</pre>
<h3 id="accessors-hash">hash(), fragment()</h3>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html#world");
// get hash
uri.hash(); // returns string "#world" (leading #)
// get fragment
uri.fragment(); // returns string "world" (no leading #)
// .hash() and .fragment() behave the same for the following:
// set hash
uri.hash("#mars"); // returns the URI instance for chaining
uri.hash("mars"); // returns the URI instance for chaining
// uri == "http://example.org/bar/world.xml#mars"</pre>
<h3 id="search-add">addSearch(), addQuery()</h3>
<p>.addQuery() is an alias of .addSearch()</p>
<pre class="prettyprint lang-js">var uri = new URI("?hello=world");
uri.addSearch("hello", "mars"); // returns the URI instance for chaining
// uri == "?hello=world&hello=mars"
uri.addSearch({ foo: "bar", goodbye : ["world", "mars"] });
// uri == "?hello=world&hello=mars&foo=bar&goodbye=world&goodbye=mars"
</pre>
<h3 id="search-remove">removeSearch(), removeQuery()</h3>
<p>.removeQuery() is an alias of .removeSearch()</p>
<pre class="prettyprint lang-js">var uri = new URI("?hello=world&hello=mars&foo=bar");
// remove an attribute
uri.removeSearch("hello"); // returns the URI instance for chaining
// uri == "?foo=bar"
// remove an attribute with value filter
uri.search("?hello=world&hello=mars&foo=bar");
uri.removeSearch("hello"); // returns the URI instance for chaining
// uri == "?hello=mars&foo=bar"
// remove multiple values
uri.search("?hello=world&hello=mars&foo=bar&mine=true");
uri.addSearch(["hello", "foo"]);
// uri == "?mine=true"
// remove multiple values with value filter
uri.search("?hello=world&hello=mars&foo=bar&mine=true&a=1&b=2&b=3");
uri.addSearch({hello: "world", foo: undefined, a: ["1", "3"]});
// uri == "?hello=mars&mine=true&b=2"</pre>
<h3 id="normalize">normalize()</h3>
<p>executes normalizeHostname(), normalizePort(), normalizePath(), normalizeSearch(), normalizeHash()</p>
<h3 id="normalize-host">normalizeHostname()</h3>
<p>For IDN conversion <a href="https://github.com/bestiejs/punycode.js">punycode.js</a> must be available (bundled in URI.js).
For IPv6-best-notation conversion IPv6.js must be available (bundled in URI.js).</p>
<pre class="prettyprint lang-js">var uri = new URI("http://www.exämple.org/");
// normalize IDN host
uri.normalizeHostname(); // returns the URI instance for chaining
// uri == "http://www.xn--exmple-cua.org/"
// normalize IPv6 host
uri.hostname('fe80:0000:0000:0000:0204:61ff:fe9d:f156');
uri.normalizeHostname(); // returns the URI instance for chaining
// uri == "http://fe80::204:61ff:fe9d:f156/"
</pre>
<h3 id="normalize-port">normalizePort()</h3>
<p>Removes the port, if it's the default for the given protocol (http: 80, https: 443, ftp: 21).</p>
<p>The list of default ports can be modified at <code>URI.defaultPorts</code></p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org:80/foo.html");
// normalize port
uri.normalizePort(); // returns the URI instance for chaining
// uri == "http://example.org/foo.html"</pre>
<h3 id="normalize-path">normalizePathname(), normalizePath()</h3>
<p>.normalizePath() is an alias of .normalizePathname(), they resolve relative hierarchies</p>
<pre class="prettyprint lang-js">var uri = new URI("/hello/foo/woo/.././../world.html");
// normalize path
uri.normalizePathname(); // returns the URI instance for chaining
// uri == "/hello/world.html"</pre>
<h3 id="normalize-search">normalizeSearch(), normalizeQuery()</h3>
<p>Turns <code>?&foo=bar&&foo=bar&foo=baz&</code> into <code>?foo=bar&foo=baz</code> and removes ? if there is no query string.</p>
<pre class="prettyprint lang-js">var uri = new URI("?&foo=bar&&foo=bar&foo=baz&");
// normalize search
uri.normalizeSearch(); // returns the URI instance for chaining
// uri == "?foo=bar&foo=baz"</pre>
<h3 id="normalize-hash">normalizeHash(), normalizeFragment()</h3>
<p>removes # if there is no hash</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org/foo/hello.html#");
// normalize hash
uri.normalizeHash(); // returns the URI instance for chaining
// uri == "http://example.org/bar/world.xml"</pre>
<h3 id="relativeto">relativeTo()</h3>
<p>.relativeTo() compares to paths an makes one relative to the other</p>
<pre class="prettyprint lang-js">var uri = new URI("/relative/path");
// make path relative
var relUri = uri.relativeTo("/relative/sub/foo/sub/file"); // returns a new URI instance
// relUri == "../../../path"</pre>
<p>.relativeTo() and .absoluteTo() reverse each other.</p>
<h3 id="absoluteto">absoluteTo()</h3>
<p>.absoluteTo() makes a relative path absolute based on another path</p>
<pre class="prettyprint lang-js">var uri = new URI("../../../path");
// make path absolute
var relUri = uri.absoluteTo("/relative/sub/foo/sub/file"); // returns a new URI instance
// relUri == "/relative/path"</pre>
<p>.relativeTo() and .absoluteTo() reverse each other.</p>
</div>
</body>
</html>