Search code examples
javascriptjqueryjquery-mobilehashchange

Jquery mobile - onhashchange issue


I am using $.mobile in my app. I must create my own routing system. I bind observer on hashchange and I pull out interesting data from location.hash. I have a problem - jQuery.mobile removes the hash sign from location.hash if it has a slashes ( e.g. from 'lalal/#controller/action/param' to 'lalal/controller/action/param' and $.mobile says in yellow box Error Loading Page.

I tried to unbind existing "hashchange" in first, but then pages not load automatically ( what I require ).

How to prevent changes of hash, but that jQuery must still load the page automatically( e.g. by its ID declared in element having data-role='page')? . Below is a fragment of my router class: ( Router.load doesn't change location.hash )

__construct: function() {   

        var that = this; 
        $( window ).bind( "hashchange" , function( e ) {
            //e.stopImmediatePropagation()
            that.load( this.location.hash  ); 

        });  
    }

Solution

  • I believe you are fighting against the "pushState" plugin in jQuery Mobile added in Beta 3 (I believe). You can disable this plugin with the following code (used before you include the jQuery Mobile JavaScript file):

    $(document).on('mobileinit', function () {
        $.mobile.pushStateEnabled = false;
    });
    

    Check-out the documentation here (notice the "pushState Plugin" section): http://jquerymobile.com/demos/1.0rc3/docs/pages/page-navmodel.html