Plexus81
Plexus81

Reputation: 1251

JQuery Mobile how to run script only once on pageinit

I've got a problem with jquery mobile and nested-lists. When I load the page for the first time it looks great, but when I navigate in the jquery mobile list it dosn't run the script because it's in the document.ready()

I found a solution using

$('[data-role="page"]').live('pageinit', function (event) {
}

but then the script runs 6 times because I've got 6 data-role="page" on the site. How can I manage to run a script only once on "pageinit".

I'm trying to manipulate a page, therefore I wan't to just select the first one / run the code only once.

Upvotes: 1

Views: 2880

Answers (1)

Daniel Lorenz
Daniel Lorenz

Reputation: 4336

Not sure if this is your problem, but it was mine. Two things. First, make sure that you didn't accidentally reference the jQuery Mobile javascript file more than once. That causes bad things to happen. Second, you might want to add this to your first page:

$(document).bind("mobileinit", function () {
    // As of Beta 2, jQuery Mobile's Ajax navigation does not work in all cases (e.g.,
    // when navigating from a mobile to a non-mobile page), hence disabling it.
    $.mobile.ajaxEnabled = false;
});

Then you will want to use:

$("#pageName").bind("pageinit", function (event) {
   // Start code here
});

Upvotes: 1

Related Questions