14

Ilya Grigorik recommends the use of the <script async> tag where possible.

Is there a clean, preferred way to load an Angular.js app using the tag, without using tools like require.js or the $script.js tool recommended by the angular-seed?

The obvious issue is execution order. e.g. preventing:

Uncaught ReferenceError: angular is not defined

https://www.igvita.com/2014/05/20/script-injected-async-scripts-considered-harmful/

2
  • 1
    i'd use defer instead of async.With defer the execution order is guaranteed.
    – mpm
    Commented May 22, 2014 at 17:53
  • The answer may be given by the question "What works in the app without angular". For me, the answer is "nothing". So there's no point in deferring its loading.
    – maaartinus
    Commented May 27, 2014 at 18:57

4 Answers 4

2

If there's something useful you can show without angular (e.g., a pre-generated content using a headless browser) AND nothing but your script depends on it1, then there can be a solution:

  • load angular.js using async
  • encapsulate you code in a function body
  • add a loop checking if angular is defined
    • and sleeping for a few milliseconds if not
    • otherwise, executing your code and breaking out of the loop

This sort of busy waiting is ugly, but I can't see how to get called back when the loading finishes. It may work or not, I haven't tried it yet.

It's quite possible that I'm doing nothing but a primitive version of what the frameworks do.

If you don't need it to work in all browsers, then there's the defer tag. To me, defer looks like async done right.


1 So I guess, users of angular-route.js or alike are out of luck. This is just a guess as I've never tried to load the two out of order.

1
  • 1
    callback function can be called like this when any scripts finished loading "<script src="abc.js" onload="callbackFunction()"></script>"
    – Aniket
    Commented Oct 11, 2018 at 11:40
1

According to this answer you might want to try to use defer instead of async, which leads to the browser to respect the loading order. E.g.:

<script src="scripts/vendor-including-angular.js" defer></script>    
<script src="scripts/your-app.js" defer></script>
0
0

You may want to merge all dependent JS files into one before using async. Use Grunt, Gulp, Broccoli or another task runner for this.

If the script is modular and does not rely on any scripts then use async.

Source: Frontend performance tips for web developers.

0

DON'T USE async attribute with Angular.js script tag.

It seems that using async attribute is harmful. Without it, expressions are evaluated on DOMContentLoaded event, but with the async attribute, there are evaluated on window load event, that is much later. Tested for Angular.js 1.0 and 1.4 in latest version of Firefox, Chrome and IE11.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.