UNPKG

axios

Version:

Promise based HTTP client for the browser and node.js

50 lines (44 loc) 1.62 kB
'use strict'; var msie = /(msie|trident)/i.test(navigator.userAgent); var utils = require('./utils'); var urlParsingNode = document.createElement('a'); var originUrl = urlResolve(window.location.href); /** * Parse a URL to discover it's components * * @param {String} url The URL to be parsed * @returns {Object} */ function urlResolve(url) { var href = url; if (msie) { // IE needs attribute set twice to normalize properties urlParsingNode.setAttribute('href', href); href = urlParsingNode.href; } urlParsingNode.setAttribute('href', href); // urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils return { href: urlParsingNode.href, protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '', host: urlParsingNode.host, search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '', hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '', hostname: urlParsingNode.hostname, port: urlParsingNode.port, pathname: (urlParsingNode.pathname.charAt(0) === '/') ? urlParsingNode.pathname : '/' + urlParsingNode.pathname }; } /** * Determine if a URL shares the same origin as the current location * * @param {String} requestUrl The URL to test * @returns {boolean} True if URL shares the same origin, otherwise false */ module.exports = function urlIsSameOrigin(requestUrl) { var parsed = (utils.isString(requestUrl)) ? urlResolve(requestUrl) : requestUrl; return (parsed.protocol === originUrl.protocol && parsed.host === originUrl.host); };