Faizan Ali
Faizan Ali

Reputation: 509

how to show loader in non ajax navigation in jquery mobile?

I ma using non ajax based navigation in jquery mobile because I want to load and run a specific script on each page. But the problem is that When I use the non ajax based navigation in jquery mobile. The loader doesn't show up. I disabled the ajax by using

data-ajax="false"

with every anchor link.

Is there any quick way to show loader with each page transition without having to write custom function ?

Upvotes: 1

Views: 3384

Answers (2)

codaniel
codaniel

Reputation: 5253

I don't think that a spinner with ajax disabled is possible. I mean you could flash it for a second before or after the page is loaded but that kinda defeats the purpose. I do however know that loading and running specific script on specific pages is possible. So maybe your question should be how do I get specific scripts to run in specific JQM pages?

Binding to pageinit will help you execute your own javascript for specific pages. The following will only execute when a JQM page with the id of page2 is loaded. Just put this in an external script and link to it in the head of your pages.

$(document).on('pageinit','#page2',function(){
    $('#appendMe').append('This text was appended');    
});

If you want to load an external script/library use the $.getScript(); method. In my example I am going to load and execute the spin.js library when a JQM page with the id of page3 is loaded. Spin.js just puts a little spinner in the page.

​​$(document).on('pageinit','#page3',function(){
    $.getScript('http://fgnass.github.com/spin.js/dist/spin.min.js', function(){
        //the following code gets executed after spin.js is loaded
        var opts = {
            lines: 13, // The number of lines to draw
            length: 7, // The length of each line
            width: 4, // The line thickness
            radius: 10, // The radius of the inner circle
            rotate: 0, // The rotation offset
            color: '#000', // #rgb or #rrggbb
            speed: 1, // Rounds per second
            trail: 60, // Afterglow percentage
            shadow: false, // Whether to render a shadow
            hwaccel: false, // Whether to use hardware acceleration
            className: 'spinner', // The CSS class to assign to the spinner
            zIndex: 2e9, // The z-index (defaults to 2000000000)
            top: 'auto', // Top position relative to parent in px
            left: 'auto' // Left position relative to parent in px
        };
        var target = document.getElementById('spin');
        var spinner = new Spinner(opts).spin(target);
    });
});​​​​​​​​​​​​​​​​

Here is a jsfiddle to convince you I'm not just making this all up. Hehe

Upvotes: 2

Joanes
Joanes

Reputation: 187

There is a client-side method to show/hide the loader:

$.mobile.showPageLoadingMsg();

$.mobile.hidePageLoadingMsg();

Upvotes: 1

Related Questions