Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the second page as the default page in a jQuery mobile Multi-page template structure?

How can I show the second page as the default page in a jQuery mobile Multi-page template structure?

<body> 
<div data-role="page" id="foo">
    <div data-role="header">
        <h1>Foo</h1>
    </div>
    <div data-role="content">   
        <p>I'm first in the source order so I'm shown as the page.</p>      
    </div>
</div>
<div data-role="page" id="home">
    <div data-role="header">
        <h1>Home</h1>
    </div>
    <div data-role="content">   
        <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>      
    </div>
</div>
<div data-role="page" id="bar">
    <div data-role="header">
        <h1>Bar</h1>
    </div>
    <div data-role="content">   
        <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>      
    </div>
</div>
</body>
like image 996
Mithun Sreedharan Avatar asked May 24 '13 11:05

Mithun Sreedharan


People also ask

What is basic structure of jQuery Mobile page?

Inside the body: Pages Within the "page" container, any valid HTML markup can be used, but for typical pages in jQuery Mobile, the immediate children of a "page" are divs with data-roles of "header" , "content" , and "footer" .

How do you include multiple pages in the single jQuery Mobile document explain with an example?

Multiple pages can be included in the single jQuery mobile document which loads together by adding multiple divs with the attribute data-role = "page". The div with data-role = "page" should consist unique id to link internally between the pages.

What is viewport in jQuery Mobile?

jQuery Mobile CDN: The viewport <meta> tag inside the <head> element specifies how the browser should control the page zoom level and dimensions. The width=device-width sets the width of the page to follow the screen-width of the device (which will vary depending on the device).


1 Answers

Example

Working example: http://jsfiddle.net/Gajotres/UfDaf/

Code used

This part will prevent normal page load. Don't forget, like in my example mobileinit MUST be initialized before jQuery Mobile is initialized.

$(document).on("mobileinit",function() {
    $.mobile.autoInitializePage = false;
}); 

This part will start manual page initialization:

$(document).ready(function() {            
    window.location.hash = 'home';
    $.mobile.initializePage();
});

While I usually advise against of document ready usage here we need it to kick start the manual change.

Full code example

In case jsFiddle is down.

HTML :

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script>
            $(document).on("mobileinit",function() {
                $.mobile.autoInitializePage = false;
            });        
        </script>
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
    </head>
    <body>
        <div data-role="page" id="foo">
            <div data-role="header">
                <h1>Foo</h1>
            </div>
            <div data-role="content">   
                <p>I'm first in the source order so I'm shown as the page.</p>      
            </div>
        </div>
        <div data-role="page" id="home">
            <div data-role="header">
                <h1>Home</h1>
            </div>
            <div data-role="content">   
                <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>      
            </div>
        </div>
        <div data-role="page" id="bar">
            <div data-role="header">
                <h1>Bar</h1>
            </div>
            <div data-role="content">   
                <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>      
            </div>
        </div>        
    </body>
</html>   

Javascript :

$(document).ready(function() {            
    window.location.hash = 'home';
    $.mobile.initializePage();
});
like image 146
Gajotres Avatar answered Oct 03 '22 22:10

Gajotres