jQM - Prevent page transition

by tronc

HTML

<link rel="stylesheet" href="http://jeromeetienne.github.com/jquery-mobile-960/css/jquery-mobile-fluid960.css">
<script src="http://timeago.yarp.com/jquery.timeago.js"></script>
<script src="http://imakewebthings.github.com/jquery-waypoints/waypoints.min.js"></script>

        <div data-role="page" id="index">
            <div data-theme="b" data-role="header">
                <h1>Index page</h1>
            </div>
            
            <div data-role="content">
                <a data-role="button" href="#second">To second page</a>
            </div>
        </div>
        <div data-role="page" id="second">
            <div data-theme="b" data-role="header">
                <h1>Second page</h1>
            </div>
            
            <div data-role="content">

            </div>
        </div>   
        <div data-role="page" id="third">
            <div data-theme="b" data-role="header">
                <h1>Third page</h1>
            </div>
            
            <div data-role="content">

            </div>
        </div>

JavaScript

$(document).on('pagebeforechange', function(e, data){  
    var to = data.toPage;
    var 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 === '#index' && to === '#second') {
            e.preventDefault();
            e.stopPropagation();            
            $.mobile.changePage( "#third");
        }
    }
});