2

I am using jQuery Mobile and I would like to redirect the browser to another page, after the user clicked on a button on the home. To do this I wrote:

$.mobile.changePage("album-search-results.html",{
               data:{area:searchArea, value:searchValue},
               transition: "pop"
           });

And it works fine, it loads the correct page and it even puts the right values on the URL. Unfortunately the pageshow event is not triggered:

    <!DOCTYPE html>
<html>
<head>


    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
</head>

<body>
    <div data-role = "page" data-theme = "d" id = "SearchResults">

        <div data-role = "header">
            <h1>Aggregator</h1>
        </div>

        <div data-role = "content">

        </div>
    </div>

    <script type="text/javascript">
        $("#SearchResults").on("pageshow",function(event, ui){
            console.log(ui.prevPage);

        });

    </script>
</body>
</html>

The console in always empty when I load this page from the previous one. What is wrong? Thanks

Gajotres
  • 57,309
  • 16
  • 102
  • 130
user1012480
  • 752
  • 2
  • 11
  • 24
  • I thought pageshow was only an event for target document? https://developer.mozilla.org/en-US/docs/Web/Events/pageshow – NoBugs Jan 09 '15 at 06:49

1 Answers1

4

Solution

Solution is simple, move script block inside a page div. In your case script block is discarded. Basically change it like this:

From :

<body>
    <div data-role = "page" data-theme = "d" id = "SearchResults">

        <div data-role = "header">
            <h1>Aggregator</h1>
        </div>

        <div data-role = "content">

        </div>
    </div>

    <script type="text/javascript">
        $("#SearchResults").on("pageshow",function(event, ui){
            console.log(ui.prevPage);

        });

    </script>
</body>

To :

<body>
    <div data-role="page" data-theme="d" id="SearchResults">
        <div data-role = "header">
            <h1>Aggregator</h1>
        </div>
        <div data-role = "content">

        </div>
        <script>
            $(document).on("pageshow","#SearchResults",function(event, ui){
                console.log(ui.prevPage);
            });
        </script>       
    </div>
</body>

Example:

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <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.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>      
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
        <script>
            $(document).on('click', '#change-page', function(){       
                $.mobile.changePage("album-search-results.html",{
                    data:{area:"asda", value:"1"},
                    transition: "pop"
                });
            });                 
        </script>
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="a" data-role="header">
                <h3>
                    First Page
                </h3>
                <a href="#second" class="ui-btn-right">Next</a>
            </div>

            <div data-role="content">
                <a data-role="button" id="change-page">Change Page</a>
            </div>

            <div data-theme="a" data-role="footer" data-position="fixed">

            </div>
        </div>    
    </body>
</html>   

album-search-results.html

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
</head>
    <body>
        <div data-role="page" data-theme="d" id="SearchResults">
            <div data-role = "header">
                <h1>Aggregator</h1>
            </div>
            <div data-role = "content">

            </div>
            <script>
                $(document).on("pageshow","#SearchResults",function(event, ui){
                    console.log(ui.prevPage);
                });
            </script>       
        </div>
    </body>
</html>
Gajotres
  • 57,309
  • 16
  • 102
  • 130