Handle loading script error with <script> onload and onerror method - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Script

Description

Handle loading script error with <script> onload and onerror method

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//from  w w  w  . j  a v a 2s.  co  m
var script = document.createElement('script');
document.getElementsByTagName("head")[0].appendChild(script);
script.onload = function() {
    console.log('loaded');
}
script.onerror = function() {
    console.log('foo!');
}
script.src = 'http://wrongServerYourAAAAAAA.com/script.js';


    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials