Vikas Bansal Vikas Bansal - 1 year ago 39
Javascript Question

Javascript: How to add call back that notify after loading a javascript file "async defer"

I am loading

in my app asynchronously by using
async defer

<script src="" async defer>

I need a callback that will notify me that the lib is loaded successfully or there is an error in loading.

Is it possible to add a callback in the Script tag?

Also, as there is a
then there must be a
, is't it?

Answer Source

You can define an onload callback that will execute when the script has successfully loaded. You can use this callback to maker sure your scripts run at the appropriate time in your application.

<script src="" async defer>

Please remember, in case you load the script synchronously, your onload callback function must be defined prior to loading the script above in your source code.

Working example (asynchronous):,console,output

<!DOCTYPE html>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
<script src="" async defer></script>
  function onLoadCallback(){
    alert('your file has been loaded');