PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/pages/page-scripting.html

https://github.com/inorganik/jquery-mobile
HTML | 127 lines | 117 code | 10 blank | 0 comment | 0 complexity | cd8d21bbc9a5d3e96112212b63c2258e MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>jQuery Mobile Docs - Scripting pages</title>
  7. <link rel="stylesheet" href="../../themes/default/" />
  8. <link rel="stylesheet" href="../_assets/css/jqm-docs.css"/>
  9. <script src="../../js/jquery.js"></script>
  10. <script src="../../experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
  11. <script src="../_assets/js/jqm-docs.js"></script>
  12. <script src="../../js/"></script>
  13. </head>
  14. <body>
  15. <div data-role="page" class="type-interior">
  16. <div data-role="header" data-theme="f">
  17. <h1>Scripting pages</h1>
  18. <a href="../../" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
  19. </div><!-- /header -->
  20. <div data-role="content">
  21. <div class="content-primary">
  22. <p>Since jQuery Mobile uses an Ajax-powered navigation system, there are a few helpful things to know when writing scripts that interact with the page and navigation model. You can explore the mobile API in more detail by reading up on <a href="../api/globalConfig.html">global configuration options</a>, <a href="../api/events.html">events</a>, and <a href="../api/methods.html">methods</a> or dig into the technical details of the <a href="page-navmodel.html">Ajax navigation model</a>.</p>
  23. <h2>Scripts & styles in the head</h2>
  24. <p>When you click a link in jQuery Mobile, the Ajax navigation system uses the link's href to formulate an Ajax request. Although the full page is loaded with Ajax, the framework only pulls in the <em>contents of the page</em>, and ignores anything in the <code>head</code> except for title tag contents.</p>
  25. <p> This means that any scripts or styles in the <code>head</code> of the page won't have any effect <em>when the page is loaded via Ajax</em>. The same page will work as expected if you were to load a page directly but both scenarios need to be considered. The reason that the <code>head</code> is ignored for Ajax page content: it's just too complex. The framework would need to compare and reconcile the contents of multiple page <code>head</code> elements as they are loaded into the DOM so we leave this task to the developer.</p>
  26. <p>The simplest approach is to add the same set of stylesheets and scripts into all your pages. If you need to load in specific scripts or styles for a particular page, bind to the <code>pagecreate</code> event (details below) to run the necessary code when a specific page ID is created. Following this approach will ensure that the code executes if the page is loaded directly or is pulled in and shown via Ajax.</p>
  27. <h2>pagecreate = DOM ready</h2>
  28. <p>The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created by the Ajax navigation system, you must bind to the <a href="../api/events.html"><code>pagecreate</code></a> event. </p>
  29. <p>The <code>pagecreate</code> event is triggered on the page being initialized, right after initialization occurs. We recommend binding to this event instead of DOM ready() because this will work regardless of whether the page is loaded directly or if the content is pulled into another page as part of the Ajax navigation system.</p>
  30. <pre><code>
  31. $('#aboutPage').live('pagecreate',function(event){
  32. alert('This page was just enhanced by jQuery Mobile!');
  33. });
  34. </code></pre>
  35. <h2>Changing pages</h2>
  36. <p>If you need to change the current page with JavaScript, use the <a href="../api/methods.html"><code>changePage</code></a> method. There are a lot of methods and properties that you can set when changing pages, but here are two simple examples:</p>
  37. <pre><code>
  38. <strong>//transition to the "about us" page with a slideup transition</strong>
  39. $.mobile.changePage( "about/us.html", { transition: "slideup"} );
  40. <strong>//transition to the "search results" page, using data from a form with an ID of "search"" </strong>
  41. $.mobile.changePage( "searchresults.php", {
  42. type: "post",
  43. data: $("form#search").serialize()
  44. });
  45. </code></pre>
  46. <h2>Loading pages</h2>
  47. <p>To load an external page, enhance its content, and insert it into the DOM, use the <a href="../api/methods.html"><code>loadPage</code> method</a>. There are a lot of methods and properties that you can set when loading pages, but here is a simple example:</p>
  48. <pre><code>
  49. //load the "about us" page into the DOM
  50. $.mobile.loadPage( "about/us.html" );
  51. </code></pre>
  52. <h2>Enhancing new markup</h2>
  53. <p>The page plugin dispatches a &#8220;pagecreate&#8221; event, which most widgets use to auto-initialize themselves. As long as a widget plugin script is referenced, it will automatically enhance any instances of the widgets it finds on the page.</p>
  54. <p>However, if you generate new markup client-side or load in content via Ajax and inject it into a page, you can trigger the <code>create</code> event to handle the auto-initialization for all the plugins contained within the new markup. This can be triggered on any element (even the page div itself), saving you the task of manually initializing each plugin (listview button, select, etc.).</p>
  55. <p>For example, if a block of HTML markup (say a login form) was loaded in through Ajax, trigger the <code>create</code> event to automatically transform all the widgets it contains (<a href="http://jquerymobile.com/test/docs/forms/texts/index.html">inputs</a> and <a href="http://jquerymobile.com/test/docs/buttons/index.html">buttons</a> in this case) into the enhanced versions. The code for this scenario would be:</p>
  56. <pre style="margin: 25px 0;"><code style="font-size: 12px;">$( ...new markup that contains widgets... ).appendTo( ".ui-page" ).trigger( "create" );
  57. </code></pre>
  58. <h2>Create vs. refresh: An important distinction</h2>
  59. <p>Note that there is an important difference between the <code>create</code> event and <code>refresh</code> method that some widgets have. The <code>create</code> event is suited for enhancing <em>raw markup</em> that contains one or more widgets. The <code>refresh</code> method should be used on existing (already enhanced) widgets that have been manipulated programmatically and need the UI be updated to match.</p>
  60. <p>For example, if you had a page where you dynamically appended a new unordered list with <code>data-role=listview</code> attribute after page creation, triggering <code>create</code> on a parent element of that list would transform it into a <a href="http://jquerymobile.com/test/docs/lists/index.html">listview</a> styled widget. If more list items were then programmatically added, calling the listview&#8217;s <code>refresh</code> method would update just those new list items to the enhanced state and leave the existing list items untouched.</p>
  61. <h2>Scrolling to a position within a page</h2>
  62. <p>Since we use the URL hash to preserve Back button behavior, using page anchors to jump down to a position on the page isn't supported by using the traditional anchor link (#foo). Use the <a href="../api/methods.html"><code>silentScroll</code></a> method to scroll to a particular Y position without triggering scroll event listeners. You can pass in a <code>yPos</code> arguments to scroll to that Y location. For example:</p>
  63. <pre><code>
  64. //scroll to Y 300px
  65. $.mobile.silentScroll(300);
  66. </code></pre>
  67. <h2>Binding to mouse and touch events</h2>
  68. <p>One inportant consideration in mobile is handling mouse and touch events. These events differ significantly across mobile platforms, but the common denominator is that click events will work everywhere, but usually after a significant delay of 500-700ms. This delay is necessary for the browser to wait for double tap, scroll and extended hold tap events to potentially occur. To avoid this delay, it's possible to bind to touch events (ex. touchstart) but the issue with this approach is that some mobile platforms (WP7, Blackberry) don't support touch. To compound this issue, some platforms will emit <em>both</em> touch and mouse events so if you bind to both types, duplicate events will be fired for a single interaction.</p>
  69. <p>Our solution is to create a set of <a href="../api/events.html">virtual events</a> that normalize mouse and touch events. This allows the developer to register listeners for the basic mouse events, such as mousedown, mousemove, mouseup, and click, and the plugin will take care of registering the correct listeners behind the scenes to invoke the listener at the fastest possible time for that device. This still retains the order of event firing in the traditional mouse environment, should multiple handlers be registered on the same element for different events. The virtual mouse system exposes the following virtual events to jQuery bind methods: <code>vmouseover</code>, <code>vmousedown</code>, <code>vmousemove</code>, <code>vmouseup</code>, <code>vclick</code>, and <code>vmousecancel</code></p>
  70. </div><!--/content-primary -->
  71. <div class="content-secondary">
  72. <div data-role="collapsible" data-collapsed="true" data-theme="b">
  73. <h3>More in this section</h3>
  74. <ul data-role="listview" data-theme="c" data-dividertheme="d">
  75. <li data-role="list-divider">Pages &amp; Dialogs</li>
  76. <li><a href="page-anatomy.html">Anatomy of a page</a></li>
  77. <li><a href="page-template.html" data-ajax="false">Single page template</a></li>
  78. <li><a href="multipage-template.html" data-ajax="false">Multi-page template</a></li>
  79. <li><a href="page-titles.html">Page titles</a></li>
  80. <li><a href="page-links.html">Linking pages</a></li>
  81. <li><a href="page-transitions.html" data-ajax="false">Page transitions</a></li>
  82. <li><a href="page-dialogs.html">Dialogs</a></li>
  83. <li><a href="page-cache.html">Prefetching &amp; caching pages</a></li>
  84. <li><a href="page-navmodel.html">Ajax, hashes &amp; history</a></li>
  85. <li><a href="page-dynamic.html">Dynamically Injecting Pages</a></li>
  86. <li data-theme="a"><a href="page-scripting.html">Scripting pages</a></li>
  87. <li><a href="pages-themes.html">Theming pages</a></li>
  88. </ul>
  89. </div>
  90. </div>
  91. </div><!-- /content -->
  92. <div data-role="footer" class="footer-docs" data-theme="c">
  93. <p>&copy; 2011 The jQuery Project</p>
  94. </div>
  95. </div><!-- /page -->
  96. </body>
  97. </html>