resolve-url.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2014 Simon Lydell
  2. // X11 (“MIT”) Licensed. (See LICENSE.)
  3. void (function(root, factory) {
  4. if (typeof define === "function" && define.amd) {
  5. define(factory)
  6. } else if (typeof exports === "object") {
  7. module.exports = factory()
  8. } else {
  9. root.resolveUrl = factory()
  10. }
  11. }(this, function() {
  12. function resolveUrl(/* ...urls */) {
  13. var numUrls = arguments.length
  14. if (numUrls === 0) {
  15. throw new Error("resolveUrl requires at least one argument; got none.")
  16. }
  17. var base = document.createElement("base")
  18. base.href = arguments[0]
  19. if (numUrls === 1) {
  20. return base.href
  21. }
  22. var head = document.getElementsByTagName("head")[0]
  23. head.insertBefore(base, head.firstChild)
  24. var a = document.createElement("a")
  25. var resolved
  26. for (var index = 1; index < numUrls; index++) {
  27. a.href = arguments[index]
  28. resolved = a.href
  29. base.href = resolved
  30. }
  31. head.removeChild(base)
  32. return resolved
  33. }
  34. return resolveUrl
  35. }));