hasSameOrigin

Still undecided on the function name. This is really a test of the Same Origin Policy which governs XHR and some other browser operation.

by John Schulz

JavaScript

var hasSameOrigin = (function (window, document, undefined) {

    var page = document.location,
        protocol = page.protocol,
        domain = document.domain,
        port = page.port ? ':' + page.port : '',
        sop_string = protocol + '//' + domain + port,
        sop_regex = new RegExp('^' + sop_string),
        http_regex = /:\/\//,
        data_regex = /^data:/,
        closure = function (url) 
        {
            var is_local = (!http_regex.test(url)) || data_regex.test(url),
                is_same_origin = sop_regex.test(url);

            return is_local || is_same_origin;
        };

        return closure;
})( window, document );