title: Javascript onload and script callback functions, which takes the precedence?
body: <p>I'm loading an external script that uses callback function, which returns some specific data. If this data is not received error should be displayed.</p>
  
  <p>Here is the code I've made:</p>
  
  <pre><code>&lt;script&gt;
  //setting initial state so that function will only work once
  var visitors_loaded=false;  
  var my_callback = function( data ) {
      if (visitors_loaded) return 0;
      if (data) { 
          //success: callback function is called and it has a proper data
      visitors_loaded=true;
      alert(JSON.stringify(data));
      }
      else alert ('error'); //something went wrong
  };
  &lt;/script&gt;
  &lt;script onload="my_callback(null)" onerror="my_callback(null)"
  src="https://api.clicky.com/api/stats/4?site_id=32020&amp;sitekey=9a19b1a4d1171193&amp;type=visitors&amp;date=this-month&amp;output=json&amp;json_callback=my_callback"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>As you can see... many things that can go wrong with the script, so I naturally added an <strong><em>onerror</em></strong> event. This on error event actually fires if you change host name or domain of the script to something non-existent.</p>
  
  <p>However, if you only make changes to the url of the script, it can still connects to the server and fires an <strong><em>onload</em></strong> event instead. My callback function will not be called for those invalid requests, so I added an <em>onload</em> handler as well.</p>
  
  <p>Now the problem is, if all loaded normally and data was returned, it will fire both, <em>callback function</em> and <em>onload</em>. I have noticed that callback function is triggered before the <em>onload</em> and set the <strong>visitors_loaded</strong> variable so that the handler function is only called once.</p>
  
  <p>So far it works perfectly in JS fiddle and my offline site but I wonder if this is an expected behavior? Will that <em>json_callback</em> function always have precedence before the <strong><em>onload</em></strong> handler?</p>
  
  <p><a href="https://jsfiddle.net/5sfk9ht5/4/" rel="noreferrer">https://jsfiddle.net/5sfk9ht5/4/</a></p>