An alternative to cross-domain javascripting?

I am currently relying on a proxy script to solve this single-origin policy issue. it is slow and creates overhead. Not to mention, javascript is not displayed.

Is there a working alternative?

+3
source share
5 answers

Oh my god, I think the solution you are looking for is with IFRAME. However, the iframe approach is both mental and technical. I suggest you start with this guide:

Cross Domain Communication with IFrames

An alternative approach is to retrieve data from another server asynchronously using script and json tags:

<script src="http://remotesite.com/path/to/script/blah.js"></script>

script DOM innerHTML.

, , , script, DOM. :

function require (url, callback) {
    if (!isScriptLoaded(url)) { 
        document.write('<script src="' + url + '" type="text/javascript" charset="utf-8"><\/script>');

        if (callback) {
            callback();
        }
    }
}

function isScriptLoaded(src) {
    var scriptsLoaded =  {};
    var scriptTags    = document.getElementsByTagName("script");

    for (var i = 0, script; script = scriptTags[i]; i++) {
        if (script.src) { 
            scriptsLoaded[script.src] = 1;
        }
    };

    if (scriptsLoaded[src]) {
        return true; 
    }

    return false;
}

(, !)

- .

+1

, JavaScript, script src, . .

+5

iframe window.postMessage(message, origin) ( parent.postMessage iframe iframeElement.contentWindow.postMessage ) (Firefox, IE, Safari, Chrome ..) / window.name .

+2

JSON-P . jQuery JavaScript, :

http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback

, , , , JSON-P, iframes, postMessage, Flash- .

+1

If you control both domains and only care about Firefox 3.5+, you can use the XMLHttpRequest object and set permissions using Access Control .

+1
source

Source: https://habr.com/ru/post/1720982/


All Articles