In jQuery Mobile, I am attempting to run scripts on a page at load time. If certain criteria are NOT met, I redirect to another page.
For example PageA –> click link to PageB –> perform pageload logic –> Redirect to PageC.
All works well the first time I navigate to the PageB.
If I then return to PageA and repeat the navigation to PageB, the redirect does not occur (the pageload logic is not executed).
If I reload PageA and try again, the redirect does occur.
Seems like some kind of caching issue?? Or perhaps I should be using another type of page load event? How can I ensure the pageload logic is executed everytime the page is hit?
Here is a simplified version of the code. The script is called in the head after jqm and jqm mobile scripts.
$(document).bind('pageload', function (evt,data) {
// I need the url to see if the page needs the logic executed on it
var page = $.mobile.path.parseUrl(""+ data.url +"");
if (page == "PageB"){
logicRequired = "yes";
}
switch (logicRequired)
{
//==========================
case 'yes':
// AJAX call to determine if redirect is needed
$.ajax({
url: "scripts/file.php",
async: false,
cache: false,
success: function (data){
if(data.status == 'success') {
// do some stuff
} else if(data.status == 'error') {
//redirect to another page
window.location.replace('redirectPage.html');
}
},
error: function(data){
//alert("There was an error in the ajax call");
},
});
break;
}
});
pageloadonly fires once each time an external pseudo-page is brought into the DOM. So if you navigate back to that page the event will not fire again. You can however use thepagebeforeshowevent which fires on the first visit to a pseudo-page as well as subsequent visits to the pseudo-page.You may also be able to flag the specific pseudo-pages with the
data-cache="false"attribute so jQuery Mobile deletes the pseudo-page after a user navigates away from the pseudo-page.