Add Panel Dynamically

jQuery Mobile

by Omar X

HTML

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<div data-role="page" id="p1">
    <!-- header -->
    <div data-role="header"> <a href="#mypanel" data-role="button" data-inline="true" data-icon="grid" data-iconpos="notext"></a>

         <h1>Header</h1>

    </div>
    <!-- content -->
    <div data-role="content">
         <h1>contents</h1>
 <a data-role="button" href="#p2">Page 2</a>

    </div>
    <!-- footer -->
    <div data-role="footer">
         <h1>Footer</h1>

    </div>
</div>
<!-- page -->
<div data-role="page" id="p2">
    <!-- header -->
    <div data-role="header"> <a href="#mypanel" data-role="button" data-inline="true" data-icon="grid" data-iconpos="notext"></a>

         <h1>Header</h1>

    </div>
    <!-- content -->
    <div data-role="content">
         <h1>contents</h1>
 <a data-role="button" href="#p1">Page 1</a>
 <a data-role="button" href="#p3">Page 3</a>

    </div>
    <!-- footer -->
    <div data-role="footer">
         <h1>Footer</h1>

    </div>
</div>
<!-- page -->
<div data-role="page" id="p3">
    <!-- header -->
    <div data-role="header"> <a href="#mypanel" data-role="button" data-inline="true" data-icon="grid" data-iconpos="notext"></a>

         <h1>Header</h1>

    </div>
    <!-- content -->
    <div data-role="content">
         <h1>contents</h1>
 <a data-role="button" href="#p2">Page 2</a>

    </div>
    <!-- footer -->
    <div data-role="footer">
         <h1>Footer</h1>

    </div>
</div>
<!-- page -->

JavaScript

var panel = '<div data-role="panel" id="mypanel" data-position="left" data-display="push"><h1>Panel</h1><p>stuff</p></div>';

$(document).on('pagebeforecreate', function () {
    if ($(this).find('[data-role=panel]').length === 0) {
        $('[data-role=header]').before(panel);
    }
});