[6a3a178] | 1 | Overview
|
---|
| 2 | ========
|
---|
| 3 |
|
---|
| 4 | [![browser support](https://ci.testling.com/lydell/resolve-url.png)](https://ci.testling.com/lydell/resolve-url)
|
---|
| 5 |
|
---|
| 6 | Like Node.js’ [`path.resolve`]/[`url.resolve`] for the browser.
|
---|
| 7 |
|
---|
| 8 | ```js
|
---|
| 9 | var resolveUrl = require("resolve-url")
|
---|
| 10 |
|
---|
| 11 | window.location
|
---|
| 12 | // https://example.com/articles/resolving-urls/edit
|
---|
| 13 |
|
---|
| 14 | resolveUrl("remove")
|
---|
| 15 | // https://example.com/articles/resolving-urls/remove
|
---|
| 16 |
|
---|
| 17 | resolveUrl("/static/scripts/app.js")
|
---|
| 18 | // https://example.com/static/scripts/app.js
|
---|
| 19 |
|
---|
| 20 | // Imagine /static/scripts/app.js contains `//# sourceMappingURL=../source-maps/app.js.map`
|
---|
| 21 | resolveUrl("/static/scripts/app.js", "../source-maps/app.js.map")
|
---|
| 22 | // https://example.com/static/source-maps/app.js.map
|
---|
| 23 |
|
---|
| 24 | resolveUrl("/static/scripts/app.js", "../source-maps/app.js.map", "../coffee/app.coffee")
|
---|
| 25 | // https://example.com/static/coffee/app.coffee
|
---|
| 26 |
|
---|
| 27 | resolveUrl("//cdn.example.com/jquery.js")
|
---|
| 28 | // https://cdn.example.com/jquery.js
|
---|
| 29 |
|
---|
| 30 | resolveUrl("http://foo.org/")
|
---|
| 31 | // http://foo.org/
|
---|
| 32 | ```
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | Installation
|
---|
| 36 | ============
|
---|
| 37 |
|
---|
| 38 | - `npm install resolve-url`
|
---|
| 39 | - `bower install resolve-url`
|
---|
| 40 | - `component install lydell/resolve-url`
|
---|
| 41 |
|
---|
| 42 | Works with CommonJS, AMD and browser globals, through UMD.
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | Usage
|
---|
| 46 | =====
|
---|
| 47 |
|
---|
| 48 | ### `resolveUrl(...urls)` ###
|
---|
| 49 |
|
---|
| 50 | Pass one or more urls. Resolves the last one to an absolute url, using the
|
---|
| 51 | previous ones and `window.location`.
|
---|
| 52 |
|
---|
| 53 | It’s like starting out on `window.location`, and then clicking links with the
|
---|
| 54 | urls as `href` attributes in order, from left to right.
|
---|
| 55 |
|
---|
| 56 | Unlike Node.js’ [`path.resolve`], this function always goes through all of the
|
---|
| 57 | arguments, from left to right. `path.resolve` goes from right to left and only
|
---|
| 58 | in the worst case goes through them all. Should that matter.
|
---|
| 59 |
|
---|
| 60 | Actually, the function is _really_ like clicking a lot of links in series: An
|
---|
| 61 | actual `<a>` gets its `href` attribute set for each url! This means that the
|
---|
| 62 | url resolution of the browser is used, which makes this module really
|
---|
| 63 | light-weight.
|
---|
| 64 |
|
---|
| 65 | Also note that this functions deals with urls, not paths, so in that respect it
|
---|
| 66 | has more in common with Node.js’ [`url.resolve`]. But the arguments are more
|
---|
| 67 | like [`path.resolve`].
|
---|
| 68 |
|
---|
| 69 | [`path.resolve`]: http://nodejs.org/api/path.html#path_path_resolve_from_to
|
---|
| 70 | [`url.resolve`]: http://nodejs.org/api/url.html#url_url_resolve_from_to
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | Tests
|
---|
| 74 | =====
|
---|
| 75 |
|
---|
| 76 | Run `npm test`, which lints the code and then gives you a link to open in a
|
---|
| 77 | browser of choice (using `testling`).
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | License
|
---|
| 81 | =======
|
---|
| 82 |
|
---|
| 83 | [The X11 (“MIT”) License](LICENSE).
|
---|