Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess server response from same javascript function that initiated the request
    primarykey
    data
    text
    <p>With complete control over both the client and server side code, I'd like to accomplish the following:</p> <ul> <li>Initiate a server request in a javascript function</li> <li>Be able to abandon the request (from the user experience perspective) after a specified time </li> <li>Access information about the response (e.g. either a redirect URL or part of the response body) <em>before</em> exiting the original function (this part is non-negotiable; setting a window interval, for example, will not cut it)</li> </ul> <p>This sounds a lot like multithreading to me, which of course javascript doesn't do. Perhaps there's no solution, but I'm exhausting my options before admitting to that. In the non-working example below, function <code>foo()</code> sets an iframe's <code>src</code> to the url of a page -- redirect.aspx here -- which after a short delay redirects to another page with some UUID in the query string. (Note: it could just as well return the UUID in a hidden field in the response body, or via some other strategy; I have control over this).</p> <p>Regardless <em>how</em> the server page returns the result, my goal is to access the UUID from the server <strong>before</strong> <code>foo()</code> exits. </p> <p><strong>Update: Suggested Unit Test</strong> Though this question <em>appears</em> to be about scope -- and therefore solvable via closures (test pending) -- it's actually about continuity of execution. A successful test would consist of:</p> <ul> <li>Create the <code>foo()</code> function</li> <li>Assign <code>something.onClick = foo()</code></li> <li><code>foo()</code> somehow initiates a server call and retrieves a URL from the response</li> <li><code>foo()</code> then calls <code>window.open(url);</code> using that URL</li> <li>A window opens in all major browsers (critical case: IE 7, 8 &amp; 9)</li> </ul> <p>I do not currently know of a strategy that can pass this test.</p> <p><strong>Non-working sample:</strong></p> <pre><code>&lt;iframe id="aFrame" src="" height="0" width="0"&gt;&lt;/iframe&gt; &lt;script type="text/javascript" language="javascript"&gt; function foo() { var f = document.getElementById("aFrame"); var loc = "http://localhost:8080/redirect.aspx?after=1000"; f.src = loc; var start = new Date().getTime(); while (elapsedSince(start &lt; 5000)) { // allow for server response // FAIL: this is never true until after foo() exits: if (f.src != loc) { alert(encodeURIComponent(f.src)); return true; } } alert("Timeout"); return false; } function elapsedSince(startTime) { // omitted safety checks for brevity: return new Date().getTime() - startTime; } &lt;/script&gt; </code></pre> <p>I'm not an ace at Ajax functions, but according to my understanding they require a callback, which means any return information arrives outside of the initiating function. Fail.</p> <p>The above strategy doesn't work, per comments in the js code.</p> <p>Other strategies might include something like using <code>document.createElement()</code> to create the iframe and <code>.insertBefore()</code> to add it to the DOM, but I'm still not certain I would be able both to initiate that <strong>and</strong> access any response details from within the same iteration of <code>foo()</code>. </p> <p>Does anyone know of <em>any</em> strategy that meets the above criteria?</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. COI don't understand how a callback is 'fail'. The callback function has access to the enclosing function's variable's through closure. AJAX has a timeout that you can specify. AJAX has a failure callback to handle timeouts and otherwise...failure. Ajax supports asynchronous...multiple parallel requests. How does AJAX fail to handle the issues you are trying to overcome?
      singulars
    2. COBasically, it's about continuity and a single invocation, not just access to the original function's variables. Would the callback execute while `foo()` is inside the `while` loop? My (imperfect) understanding was that with an ajax callback inside a closure, `foo()` would still exit, but would re-enter via the callback. If that's untrue and `foo()` could indeed loop until the inner callback was processed, then that might be the answer indeed.
      singulars
    3. CONo, it could not, JavaScript is turn-based. When the callback is called, the enclosing function is not "re-entered". The callback function keeps a reference to the scope of the enclosing function so that it can access its variables. It's called a "closure" and it is a key feature of JavaScript. Actually, it's probably its single most awesome feature.
      singulars
 

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