Return to Snippet

Revision: 65474
at December 5, 2013 11:30 by lost


Initial Code
function checkOrigin(origin) {
    var allows = $('.allow-origin');
    for (var i = 0; i < allows.length; i++) {
        var allow = $(allows[i]).text();
        if (allow == "*")
            return true;
        if (allow == origin)
            return true;
    }
    return false;
}

function messageHandler(e) {
    var origin = e.originalEvent.origin;
    if (!checkOrigin(origin))
        return;

    var request = JSON.parse(e.originalEvent.data);
    
    // TODO remove this IF after testing
    if (origin == "file:" || origin == "null")
        origin = "*";

    $.ajax(request.ajax)
        .done(function (data) {
            request.result = data;
            var reply = JSON.stringify(request);
            parent.postMessage(reply, origin);
        })
        .fail(function (req) {
            request.error = req.statusText;
            request.errorCode = req.status;
            var reply = JSON.stringify(request);
            parent.postMessage(reply, origin);
        });
}

$(document).on('ready', function () {
    $(window).on('message', messageHandler);
});

Initial URL

                                

Initial Description
Usage:
<!DOCTYPE>

<html>
<head>
	<meta>
	<title></title>
	<script></script>
	<script></script>
</head>
<body>
	<span>https://goodguys.com</span>
	You should never see this
</body>
</html>

than just postMessage to an iframe with this HTML + JS, and wait for the reply. iframe will do request for you

Initial Title
Access Control Allow Origin simulation for IE8 and IE9

Initial Tags

                                

Initial Language
JavaScript