resolve-url.js 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. }));