Jquery mobile:加载前重定向到另一个页面

Avi*_*i C 12 jquery redirect jquery-mobile

我正在使用jquery mobile 1.0

我想从first.html重定向到otherPage.html而不显示first.html的内容

$("#pageId").live("pagebeforecreate", function(event) {                 

  window.location.href = "otherPage.html";              

});
Run Code Online (Sandbox Code Playgroud)

-i几乎尝试过所有活动,但没有成功.该first.html显示了一会儿.

然后,我尝试了'pagebeforeload'事件,但它没有被触发

$( document ).bind( "pagebeforeload", function( e, data ){ 

        console.log("pagebeforeload starting"); // NO LOGGING HAPPENING 
        window.location.href = "otherPage.html";

        e.preventDefault(); 

        data.deferred.resolve( data.absUrl, data.options, response.page); 

}); 

$( document ).bind( "pageload", function( e, data ){
    console.log("Page successfully loaded into DOM..."); // NO LOGGING HAPPENING 
});
Run Code Online (Sandbox Code Playgroud)

-am对这个事件不太清楚,并没有找到一个可行的例子. - 任何人都可以帮忙!

Sur*_* TR 12


试试这一个

$("#firstPageId").live("pagecreate",function(){
         $.mobile.changePage("otherPage.html");
});
Run Code Online (Sandbox Code Playgroud)

此外,您可以使用loadPage

希望能帮助到你 :)


Jos*_*art 4

pagebeforeload 事件仅在外部页面加载时触发:

每当外部页面加载到应用程序 DOM 中时,就会触发 2 个事件。第一个是 pagebeforeload。第二个事件将是页面加载或页面加载失败。

我解决这个问题的唯一想法是异步加载first.html的页面内容,这并不理想,因为您最终会对同一页面发出 2 个 http 请求。然而,这将使您能够仅在需要时调用first-contents.html ,否则您可以重定向到otherPage.html而不显示任何内容。例如:

<script>
    someCondition=true;
    if(someCondition) {
        window.location.href = "otherPage.html"; 
    } else {
        $(document).ready(function() {
            $('body').load('first-content.html', function() {
                //other stuff after the contents have loaded: like the rest of your jquery relating to first.html
            });
        });
    }
</script>
Run Code Online (Sandbox Code Playgroud)

确保first.html页面中的body标签为空。