Commit df25770f by David Baumgold Committed by Feanil Patel

Assign isExternal JS function to window object

When JS functions are defined with names, they are local variables, and inaccessible
if defined inside a closure. Django-Pipeline concatenates all of our JS into one
big closure. This function explicitly assings the function to a property of the
`window` object, so that it is accessible to other JS functions.
parent e30a9a6f
// checks whether or not the url is external to the local site.
// generously provided by StackOverflow: http://stackoverflow.com/questions/6238351/fastest-way-to-detect-external-urls
function isExternal(url) {
window.isExternal = function (url) {
// parse the url into protocol, host, path, query, and fragment. More information can be found here: http://tools.ietf.org/html/rfc3986#appendix-B
var match = url.match(/^([^:\/?#]+:)?(?:\/\/([^\/?#]*))?([^?#]+)?(\?[^#]*)?(#.*)?/);
// match[1] matches a protocol if one exists in the url
// if the protocol in the url does not match the protocol in the window's location, this url is considered external
if (typeof match[1] === "string" &&
match[1].length > 0
&& match[1].toLowerCase() !== location.protocol)
if (typeof match[1] === "string" &&
match[1].length > 0 &&
match[1].toLowerCase() !== location.protocol)
return true;
// match[2] matches the host if one exists in the url
// if the host in the url does not match the host of the window location, this url is considered external
if (typeof match[2] === "string" &&
match[2].length > 0 &&
if (typeof match[2] === "string" &&
match[2].length > 0 &&
// this regex removes the port number if it patches the current location's protocol
match[2].replace(new RegExp(":("+{"http:":80,"https:":443}[location.protocol]+")?$"), "") !== location.host)
match[2].replace(new RegExp(":("+{"http:":80,"https:":443}[location.protocol]+")?$"), "") !== location.host)
return true;
return false;
}
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment