EFWS home | examples | stevesouders.com
Couple XHR Eval
Examples ▼
Was the script loaded without blocking?
Was execution order preserved?

This page contains an external script and some inlined code that depends on the external script. The external script, menu.js, is loaded asynchronously. The inlined code's init function calls EFWS.Menu.createMenu(), a function defined in the external script. Because of this dependency, the inlined code must be coupled with the external script so that the execution order is preserved: the external script is loaded, parsed, and executed before the inlined code.

In this example, the script is loaded using the XHR Eval technique. (Look at the source of this document to see the full implementation.) When the response is received, it's evaluated using eval.

eval(xhrObj.responseText);

This technique loads scripts in parallel, but it doesn't preserve execution order. Therefore, the inlined code is executed before the external script finishes downloading. This results in an undefined symbol error because the inlined code references EFWS, which isn't yet defined.

Takeaway: If you use these asynchronous script loading techniques, you need to use a coupling technique to avoid undefined symbol errors.

home | contact SteveBlackbuck AntelopeThis is the companion website for Even Faster Web Sites by Steve Souders.