JQMobile pagebeforechange
pageebeforechange example
HTML
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css">
<html>
<head><title>jQuery Mobile Test</title></head>
<body>
<div id="page1" data-role="page">
<div data-role="header">
<h1>Page 1</h1>
</div>
<div data-role="content">
You should not be able to go to Page 3 from here
<a href="#page2" data-role="button">Go to Page 2</a>
<a href="#page3" data-role="button">Go to Page 3</a>
</div>
<div data-role="footer">
<h1>Footer</h1>
</div>
</div>
<div id="page2" data-role="page">
<div data-role="header">
<h1>Page 2</h1>
</div>
<div data-role="content">
<a href="#page1" data-role="button">Go to Page 1</a>
<a href="#page3" data-role="button">Go to Page 3</a>
</div>
<div data-role="footer">
<h1>Footer</h1>
</div>
</div>
<div id="page3" data-role="page">
<div data-role="header">
<h1>Page 3</h1>
</div>
<div data-role="content">
<a href="#page1" data-role="button">Go to Page 1</a>
<a href="#page2" data-role="button">Go to Page 2</a>
</div>
<div data-role="footer">
<h1>Footer</h1>
</div>
</div>
</body>
</html>
JavaScript
$(document).bind('mobileinit', function() {
$(document).bind('pagebeforechange', function(e, data) {
var to = data.toPage,
from = data.options.fromPage;
if (typeof to === 'string') {
var u = $.mobile.path.parseUrl(to);
to = u.hash || '#' + u.pathname.substring(1);
if (from) from = '#' + from.attr('id');
if (from === '#page1' && to === '#page3') {
alert('Cannot change to page 3 from page 1');
e.preventDefault();
e.stopPropagation();
// remove active status on buttons
var $active = $.mobile.activePage
.find('.ui-btn-active')
.removeClass('ui-btn-active');
}
}
});
});
// have to manually write script tag since jsFiddle loads external
// scripts before our code and we need to bind to mobilinit before
// jquery.mobile is loaded
document.write('<scr' + 'ipt type="text/javascript" src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></sc' + 'ript>');