title: JavaScript function called with button onclick, fails with onload
body: <p>I am trying to use an external JavaScript function in the body tag: <code>&lt;body onload=function()&gt;</code></p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  
  &lt;head&gt;
      &lt;script type=&quot;text/javascript&quot; src=&quot;js/mqttws31.js&quot;&gt;&lt;/script&gt;
  
      &lt;script type=&quot;text/javascript&quot; src=&quot;js/Long.min.js&quot;&gt;&lt;/script&gt;
      &lt;script type=&quot;text/javascript&quot; src=&quot;js/ByteBufferAB.min.js&quot;&gt;&lt;/script&gt;
      &lt;script type=&quot;text/javascript&quot; src=&quot;js/ProtoBuf.min.js&quot;&gt;&lt;/script&gt;
      &lt;script type=&quot;text/javascript&quot; src=&quot;js/fs.js&quot;&gt;&lt;/script&gt;
      &lt;script type=&quot;text/javascript&quot; src=&quot;js/bundle.js&quot;&gt;&lt;/script&gt;
      &lt;script type=&quot;text/javascript&quot; src=&quot;cordova.js&quot;&gt;&lt;/script&gt;
      &lt;script type=&quot;text/javascript&quot; src=&quot;cordova_plugins.js&quot;&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body onload =&quot;heartbeat()&quot;&gt;
  
  &lt;h2&gt;A demo for showing the pub/sub between clients and the message     broker&lt;/h2&gt; &lt;h3&gt;IoTGateway&lt;/h3&gt;
  Topic :
  &lt;input type=&quot;text&quot; name=&quot;pub&quot;/&gt;
  Message:
  &lt;input type=&quot;text&quot; name=&quot;pub&quot;/&gt;
  &lt;button onclick=&quot;connect()&quot;&gt;
      Connect
  &lt;/button&gt;
  &lt;br&gt;
  &lt;button onclick=&quot;publish()&quot;&gt;
      Publish
  &lt;/button&gt;
  &lt;br&gt;
  
  &lt;h3&gt;BluetoothID&lt;/h3&gt; Topic:
  &lt;input type=&quot;text&quot; name=&quot;sub&quot;/&gt;
  &lt;button onclick=&quot;heartbeat()&quot;&gt;
      Subscribe
  &lt;/button&gt;
  &lt;br&gt;
  &lt;p id = &quot;test&quot;&gt;&lt;/p&gt;
  &lt;p id=&quot;sub&quot;&gt;&lt;/p&gt;
  &lt;script&gt;
          function heartbeat() {
              alert(&quot;hello&quot;);
              try{
              MqttPlugin.subscribe({topic: &quot;$EDC/plugin&quot;});
              }
              catch(err){
              alert(err.message);
              }
          }
         /* try{
          MqttPlugin.heartbeat({topic: &quot;$EDC/tum/B8:27:EB:A6:A9:8A/HEARTBEAT-V1/mqtt/heartbeat&quot;});
          }
          catch(err){
          alert(err.message);
          }*/
  
  
  
          function publish() {
  
              MqttPlugin.publish({
              topic:&quot;$EDC/plugin&quot;,
              data:&quot;Mqtt data&quot;
  
              });
          }
  
          function subscribe() {
              MqttPlugin.subscribe({topic: &quot;$EDC/plugin&quot;});
          }
  
  
  
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>The function <code>heartbeat</code> is called when I call with the event <code>onclick</code> but fails with <code>onload</code>. In case of <code>onload</code>, it throws an error MqttPlugin not defined. Is it because by the time it calls the function, the js files are not loaded? Could someone help me fix this?</p>