Question

I am quite confused with when to call my JS code. I have the following

Zepto(function($) {
  document.addEventListener("deviceready", onDeviceReady, false);
});

function onDeviceReady() {
  initalizeData();
  FastClick.attach(document.body);
  startSnapper();
  $('input[name=topcoat]').change(function(){ schoolValueChanged() });
}

When the device is ready, deviceready is triggered. The function onDeviceReady sets up my Javascript.

Is that the right way of doing it?

Was it helpful?

Solution

In the head of your page between your script tags you should just have the event listener, it shouldn't be inside anything else. Here is how it should look:

<script>
  document.addEventListener("deviceready", onDeviceReady, false);

  function onDeviceReady() {
    initalizeData();
    FastClick.attach(document.body);
    startSnapper();
    $('input[name=topcoat]').change(function(){schoolValueChanged() });
  }
</script>

That's the way I've seen it in the documentation and that's the way I've done it in all of my Cordova projects. And if you're using any other event listeners they should be added after deviceready is called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top