Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"$(document).on('pageshow'" not working with jQuery 1.9.1 + JQM 1.3.0-stable

Working with jQuery 1.8.3

<!DOCTYPE html>
<html>
    <head>
        <script src="js/jquery-1.8.3.min.js"></script>
        <script src="js/jquery.mobile-1.3.0.min.js"></script>
        <script>
      $.support.cors = true;
      $.mobile.allowCrossDomainPages = true;

      $("#home").live("pageshow", function( event ) {
        alert( "Ok. This is Home!" );
      });
        </script>
    </head>
    <body>

    <!--- HOME --->
    <div data-role="page" id="home">
    <h2>Hello World</h2>
    </div>
    </body>
</html>

Alert works ok.

However, with jQuery 1.9.1 (please note that I changed the version and "live" with "on".

<!DOCTYPE html>
<html>
    <head>
        <script src="js/jquery-1.9.1.min.js"></script>
        <script src="js/jquery.mobile-1.3.0.min.js"></script>
        <script>
      $.support.cors = true;
      $.mobile.allowCrossDomainPages = true;

      $("#home").on("pageshow", function( event ) {
        alert( "Ok. This is Home!" );
      });
        </script>
    </head>
    <body>

    <!--- HOME --->
    <div data-role="page" id="home">
    <h2>Hello World</h2>
    </div>
    </body>
</html>

Alert won't work. I'm sure I'm doing something wrong. I've been reading that jQuery Mobile 1.3.0-stable would use jQuery 1.9.1, but perhaps I'm wrong.

like image 739
Hookstark Avatar asked Mar 17 '13 18:03

Hookstark


1 Answers

Change

 $("#home").on("pageshow", function( event ) {

to

  $(document).on("pageshow", "#home", function( event ) {

The syntax of on isn't the same than the one of live : the element that will receive and delegate the event must be existing when you make the binding.

like image 176
Denys Séguret Avatar answered Sep 24 '22 03:09

Denys Séguret