cordova - Using normal JQuery in Phonegap Project (with JQuery Mobile) -


i building app uses cordova/phonegap , jquery mobile.

i want use jquery within app, cannot work - nothing happens, simple code.

i believe have headers set correctly:

<script type="text/javascript" src="cordova-2.7.0.js"></script>         <script type="text/javascript" src="js/jquery.min.1.9.js"></script>         <script type="text/javascript" src="js/jquery.mobile-1.3.1.min.js"></script>         <script type="text/javascript" src="js/myscripts.js"></script>         <script type="text/javascript">             app.initialize();             </script> 

as jquery mobile style headers , buttons etc.

but when try simple like:

document.addeventlistener("deviceready", function(){     $('p').append("<strong>hello</strong>"); }); 

in myscripts.js file nothing. need trigger jquery in different way? point me in right direction?

edit:

i found this mentioned putting within div. worked me, eg

 <div class="normal" data-role="page" data-title="program">           <script type="text/javascript">           $(".normal").on('pageinit', function() {            $('p').append("<strong>hello</strong>");           });           </script> 

but surely theres better way? there has way have these scripts in there own external file?

ps: html is:

<body>       <div data-role="page" data-title="program">           <div id="programholder">               <div data-role="header">                   <a href="index.html" data-role="button" data-rel="back" data-direction="reverse" data-icon="arrow-l" data-iconpos="left">back</a>                   <h1>header</h1>               </div><!--header-->               <div data-role="content">                   <p>                   append here                   </p>              </div><!--content-->           </div><!--holder-->           </div><!--page-->     </body> 

to understand problem must understand how jquery mobile works. uses ajax load other pages.

first page loaded normally. head , body loaded dom, , there await other content. when second page loaded, body content loaded dom.

so if want have separated js files every page need initialize them body because head going discarded.

like this:

<body>     <script>         // javascript go here     </script>     // , rest of html content </body> 

same thing goes link , style tags.

if want find out more read other article examples regarding topic: why have put script index.html in jquery mobile

other thing, when working jquery mobile, never use kind of initialization:

document.addeventlistener("deviceready", function(){     $('p').append("<strong>hello</strong>"); }); 

nor should use classic jquery document ready. bot of them trigger before page loaded dom. why jquery mobile has page evenets. , correct using pageinit event did in wrong way. jquery mobile page event's should binded this:

$(document).on('pageinit', ".normal",function() {       $('p').append("<strong>hello</strong>"); }); 

if want find out more read other answer regarding document ready vs page events difference: jquery mobile: document ready vs page events


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -