Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery mobile: redirect to another page before loading

am using jquery mobile 1.0

I want to redirect from first.html to otherPage.html without showing content of first.html

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

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

});

-i tried almost every event, but not succeed. The first.html is shown up for a moment.

Then, I tried with 'pagebeforeload' event, but it isnt triggered

$( 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 
});

-am not much clear about this event & didn't find a workable example for it. -Can anybody plz help!

like image 532
Avisek Chakraborty Avatar asked Feb 21 '12 22:02

Avisek Chakraborty


3 Answers


Try This One

$("#firstPageId").live("pagecreate",function(){
         $.mobile.changePage("otherPage.html");
});

Also, You can use loadPage

Hope it helps :)

like image 116
Suryavel TR Avatar answered Nov 13 '22 20:11

Suryavel TR


The pagebeforeload event is only triggered when external pages are loaded in:

Whenever an external page is loaded into the application DOM, 2 events are fired. The first is pagebeforeload. The 2nd event will be either pageload or pageloadfailed.

My only idea to get around this would be to load the page contents of first.html asynchronously, which isn't ideal because you end up making 2 http requests for the same page. However this will give you the ability to only call the first-contents.html if it's needed, otherwise you can redirect to otherPage.html without anything showing. eg:

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

Make sure the body tags are empty in the first.html page.

like image 32
Josh Stuart Avatar answered Nov 13 '22 21:11

Josh Stuart


Have you tried simply not using an event. JS code will get executed as it is encountered so making sure your conditional code is at the top of the page (in the head somewhere) should be enough.

<html>
<head>
<script>
  if (condition)
  {
    window.location.href = "otherPage.html";
  }
</script>
<!-- rest of page -->
like image 1
Endophage Avatar answered Nov 13 '22 20:11

Endophage