isCrossOriginUrl.js 763 B

123456789101112131415161718192021222324252627282930
  1. import defined from "./defined.js";
  2. let a;
  3. /**
  4. * Given a URL, determine whether that URL is considered cross-origin to the current page.
  5. *
  6. * @private
  7. */
  8. function isCrossOriginUrl(url) {
  9. if (!defined(a)) {
  10. a = document.createElement("a");
  11. }
  12. // copy window location into the anchor to get consistent results
  13. // when the port is default for the protocol (e.g. 80 for HTTP)
  14. a.href = window.location.href;
  15. // host includes both hostname and port if the port is not standard
  16. const host = a.host;
  17. const protocol = a.protocol;
  18. a.href = url;
  19. // IE only absolutizes href on get, not set
  20. // eslint-disable-next-line no-self-assign
  21. a.href = a.href;
  22. return protocol !== a.protocol || host !== a.host;
  23. }
  24. export default isCrossOriginUrl;