Javascript synchronous CORS request

671 views Asked by At

Because of the cookielaw in The Netherlands, it seems we all have te implement 'cookiewalls'. My idea is quite simple, at the top of the page (but only if it's in Dutch) I made a check, if a status cookie is set, if it is the page will continue to load, if not it redirects to another page which asks for permission. When permission is granted the page redirects to it's origin. This way I didn't have to rewrite the existing JavaScript code.

I also want to check if it's set for one of our other websites. I want to do that with an AJAX call. I have found this code:

function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}

But as with all AJAX calls, it's asynchronous but in this case it has to wait until the script knows for sure it is allowed to continue, if not it has to redirect.

This XMLHttpRequest is quite easy to make synchronous I know, but seems like it's not possible with the XDomainRequest. What's the best to halt all execution until there is a response of the XDomainRequest?

0

There are 0 answers