Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use XMLHttpRequest with <a href="http://www.w3.org/TR/cors/" rel="nofollow">CORS</a> to make synchronous cross-domain requests.</p> <p>If the server doesn't support cors, use a proxy which adds the appropriate CORS headers, e.g. <a href="https://cors-anywhere.herokuapp.com/" rel="nofollow">https://cors-anywhere.herokuapp.com/</a> (source code at <a href="https://github.com/Rob--W/cors-anywhere" rel="nofollow">https://github.com/Rob--W/cors-anywhere</a>).</p> <h2>Example 1: Using synchronous XHR with CORS</h2> <pre><code>function getProvidedValue() { var url = 'http://example.com/'; var xhr = new XMLHttpRequest(); // third param = false = synchronous request xhr.open('GET', 'https://cors-anywhere.herokuapp.com/' + url, false); xhr.send(); var result = xhr.responseText; // do something with response (text manipulation, *whatever*) return result; } </code></pre> <hr> <h2>Example 2: Use <code>postMessage</code></h2> <p>If it's important to calculate the values on the fly with session data, use <code>postMessage</code> to continuously update the state:</p> <p>Top-level document (host.html):</p> <pre><code>&lt;script src="host.js"&gt;&lt;/script&gt; &lt;iframe name="content" src="https://other.example.com/content.html"&gt;&lt;/iframe&gt; </code></pre> <p><code>host.js</code></p> <pre><code>(function() { var cache = { providedValue: null, otherValue: '' }; function sendUpdate() { if (frames.content) { // "content" is the name of the iframe frames.content.postMessage(cache, 'https://other.example.com'); } } function recalc() { // Update values cache.providedValue = provideValue(); cache.otherValue = getOtherValue(); // Send (updated) values to frame sendUpdate(); } // Listen for changes using events, pollers, WHATEVER yourAPI.on('change', recalc); window.addEventListener('message', function(event) { if (event.origin !== 'https://other.example.com') return; if (event.data === 'requestUpdate') sendUpdate(); }); })(); </code></pre> <p>A script in <code>content.html</code>: content.js</p> <pre><code>var data = {}; // Global var parentOrigin = 'https://host.example.com'; window.addEventListener('message', function(event) { if (event.origin !== parentOrigin) return; data = event.data; }); parent.postMessage('requestUpdate', parentOrigin); // To get the value: function displayValue() { var hostName = data.providedValue; } </code></pre> <p>This snippet is merely a demonstration of the concept. If you want to apply the method, you probably want to split the login in the <code>recalc</code> function, such that the value is only recalculated on the update of that particular value (instead of recalculating everything on every update).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload