delek tashi
delek tashi

Reputation: 113

How to create JQuery Mobile second page to call function only once?

Actually i wish to call the function james() only once on the first click to page 2 and in the consecutive next click to 2nd page , i dont want to call the function james(). but it call every time i call page 2.

<!doctype html>
<html class="no-js" lang="">
    <head>
       

 <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">

 <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
             <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
         

 <script type="text/javascript" charset="utf-8">
               var Clicked = false;

               function james() {
                  if(Clicked === false){

                        alert("james");
                  }
                Clicked=true;      
             }
                

   </script>

    </head>
    <body>
 
    <div data-role="page" id="pagetwo" >

     <a href="#pageone" onClick="james(); this.onClick=null;">Go to Page two</a>
               
       </div>
            



    <div data-role="page" id="pageone">
  
        <a href="#pagetwo">Go to Page one</a>
  
    </div>        

    </body>
</html>

Upvotes: 1

Views: 129

Answers (3)

Gajotres
Gajotres

Reputation: 57309

This can be done easily in jQuery Mobile.

If you need to execute certain code only once during page initialization you can use jQuery mobile page event called pagecreate.

This event will trigger only once during the first page initialization.

$( document ).on( "pagecreate", "#secondPage", function( event ) {
  alert( "This page was just enhanced by jQuery Mobile!" );
});

And here's a working example: http://jsfiddle.net/Gajotres/u2tw371o/

Upvotes: 1

Rahul Sagore
Rahul Sagore

Reputation: 1658

There is .one event binding method in jquery. Don't know if that is what you need.

$( "#pageone" ).one( "click", james);

The james function is only run ONCE for each element.

Reference: http://api.jquery.com/one/

Upvotes: 0

uzain ali khan
uzain ali khan

Reputation: 1

brother just initialize your variable globaly in java script as shown below

<script type="text/javascript" charset="utf-8">
   var Clicked = false;
           function james() {
              if(Clicked === false){

                    alert("james");
              }
            Clicked=true;      
         }

Upvotes: 0

Related Questions