Jquery mobile - onhashchange issue

微笑、不失礼 提交于 2019-12-02 08:53:13

问题


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  ); 

        });  
    }

回答1:


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




回答2:


in your html after including jquery and before including jquery.mobile-1.x.y.js add:

<script>
$(document).bind("mobileinit", function(){
        $.mobile.pushStateEnabled = false;
        $.mobile.ajaxEnabled = false;
        $.mobile.hashListeningEnabled = false;
});
</script>


来源:https://stackoverflow.com/questions/8158591/jquery-mobile-onhashchange-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!