You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.4 KiB

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