1

I deal with jQuery mobile and PhoneGap at the moment and have some questions about it (documentation and books did not help).

I'm using the multi-page template to develop a mobile app. Each page has some JavaScript.

  1. Should each JavaScript begin with $(document).on("xxx")?
  2. What's the difference between document ready and $(document).on("xxx")
  3. And should I also use function onDeviceReady() in every JavaScript?
Maddy
  • 570
  • 1
  • 7
  • 27
  • Your very general questions can be answered by reading [the docs](http://api.jquery.com). You need a more specific, answerable question to have it answered on StackOverflow. Please see http://stackoverflow.com/about – Blazemonger Jan 08 '14 at 15:49
  • 1: no, only use it if you need it. 2: read the docs. 3: no, only use it if you need it. – Kevin B Jan 08 '14 at 16:04
  • Sorry, my mistake. I'm trying to do it better in the future. – Maddy Jan 08 '14 at 16:04
  • Typically if you have to ask multiple questions in a single question, the question is poorly written. All three of your questions should likely have been separate questions, but even then, 1 and 3 are too opinionated, 2 is the only one that is remotely close to being a valid question, but it's like comparing apples to oranges. – Kevin B Jan 08 '14 at 16:05

1 Answers1

3

onDeviceReady() should be used for Phonegap side, if you want to execute anything after Phonegap is loaded successfully.

Classic document ready should not be used with jQuery Mobile because in some cases it can trigger before / after page is loaded.

on method is on the other hand just method used for event binding. Do not confuse it with document ready. What you need is jQuery Mobile page evenets. Read more about them here.

Use this:

$(document).on('pageinit', function() {

});

Instead of document ready.

Several page events exists, find more about them in an official documentation, or here. Official documentation is for older version of jQuery Mobile but it also translates to last 1.4 version.

Community
  • 1
  • 1
Gajotres
  • 57,309
  • 16
  • 102
  • 130
  • Many thanks. I'm a little bit handicapped and read the documentation, but I understand it better if a human explain it to me. Can you answer my one last question. I'm on page 2. If I click on the back button to go back to page 1, I want to reload page 1. With $(document).on("xxx") should I use? – Maddy Jan 08 '14 at 16:03