RSS

Tag Archives: jquery mobile

Events are Fired Multiple Times in jQuery Mobile

I have situation where some of my events (that attach to pageshow event, or click of a button event) run multiple times in jQuery Mobile.

Following is my code in most pages. I would attach event after a page is showed.

Well, because the nature of jQuery Mobile handles page (it will keep <div data-role="page"></div> of the first page your load), every time the page is loaded, this code will be run again and again which means the events get attached multiple times.. which means they will be fired multiple times once triggered.

$(document).on("pageshow", function() {
    // attach event here
    $("#btnSubmit").click(function() {
        // code to handle event here
    });
});

Solution is simple. Instead of using .on, I changed to .one. .one ensure that the code will only be run one time.

$(document).one("pageshow", function() {
    // attach event here
    $("#btnSubmit").click(function() {
        // code to handle event here
    });
});

Version attow: jQuery Mobile 1.3.2

Advertisements
 
Leave a comment

Posted by on August 14, 2014 in General

 

Tags: ,

$(document).ready in jQuery Mobile?

Due to jQuery Mobile’s way of handling page transition (using Hijax to get pseudo page), $(document).ready is no longer a valid option. What’s the substitute?

$(document).on("pageshow", function() {
    // your code here
});

Version attow: jQuery Mobile 1.3.2

 
Leave a comment

Posted by on August 12, 2014 in General

 

Tags: ,

 
%d bloggers like this: