PageRenderTime 57ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

HTML | 144 lines | 93 code | 51 blank | 0 comment | 0 complexity | 23484e87afc26d9a677a1b3f0d1e6cfb 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 - Ajax, hashes &amp; history</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/"></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>Ajax, hashes &amp; history</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. <h2>jQuery Mobile's navigation model</h2>
  23. <p>A "page" in jQuery Mobile consists of an element (usually a <code>div</code>) with a <code> data-role</code> attribute set to <code>"page"</code>, which generally contains <code>div</code> elements with roles of <code>"header"</code>, <code>"content"</code>, and <code>"footer"</code>, each containing common markup, forms, and custom jQuery Mobile widgets.</p>
  24. <p>The basic workflow with page loading is as follows: first, a page is requested with a normal HTTP request, and subsequent "pages" are then requested and injected into that page's DOM. Because of this, the DOM may have a number of "pages" in it at a time, each of which can be re-visited by linking to its <code>data-url</code> attribute.</p>
  25. <p>When a url is initially requested, there may be one or more "pages" in the response, and only the first one will be shown. The advantage of storing more than one "page" is that it allows you to pre-fetch static pages that are likely to be visited.</p>
  26. <h2>Hash and Ajax driven page navigation</h2>
  27. <p>By default all navigation within jQuery Mobile is based on changes and updates to <code>location.hash</code>. Whenever possible, page changes will use a smooth transition between the current "page" and the next, whether it is either already present in the DOM, or is automatically loaded via Ajax.</p>
  28. <p>Hash values created by jQuery Mobile are normalized as full paths relative to the URL of the first "real" page that was loaded. The hash is always maintained as a valid URL, so any "page" in jQuery mobile can be bookmarked or referenced in a link. To retrieve a non-hash-based URL, simply remove the # from the address and refresh the page.</p>
  29. <p>In general, hash changes are created whenever a link is clicked in jQuery mobile. When a link is clicked, jQuery mobile will make sure the link is referencing a local URL, and if so, it'll prevent the link's default click behavior from occurring and request the referenced url via Ajax instead. When the page returns successfully, it will set the location.hash to the new page's relative url. </p>
  30. <p>Hash changes that occur independently of a click, such as when a user clicks the back button, are handled through the hashchange event, which is bound to the window object using Ben Alman's hashchange special event plugin (included in jQuery Mobile). When a hash change occurs (and also when the first page loads), the hashchange event handler will send the location.hash to the <code>$.mobile.changePage()</code> function, which in turn either loads or reveals the referenced page.</p>
  31. <p>Once the referenced page is present in the DOM, the <code>$.mobile.changePage()</code> function applies a transition between the current active page and the new page. Page transitions happen through adding and removing classes that apply CSS animations. For example, in a slide-left transition, the exiting page is given the classes "slideleft" and "out", and the entering page is given the classes <code>"slideleft"</code> and <code>"in"</code>, as well as a class of <code>"ui-page-active"</code> to mark it as the new "active" page being viewed. When the animation is complete, the <code>"in"</code> and <code>"out"</code> classes are removed, and the exited page loses its <code>"ui-page-active"</code> class.</p>
  32. <h2>replaceState plugin</h2>
  33. <p>For those browsers that support <code>history.pushState</code> and <code>history.replaceState</code> an additional optional plugin is provided that will convert the hash change urls mentioned in the previous section into the hashless equivelant. It's important to note that it does indeed convert the hash urls by using <code>history.replaceState</code> as the <code>history.pushState</code> does not produce the desired result in some mobile browsers.</p>
  34. <p>Since the plugin initializes when the DOM is fully loaded you can enable and disable it manually by setting <code>$.mobile.pushStateEnabled</code> anytime before.</p>
  35. <h2>changePage</h2>
  36. <p>Within the framework, page changes - both for pages already in the DOM and for pages that need to be loaded via Ajax - use the <code>$.mobile.changePage()</code> function. <code>$.mobile.changePage()</code> contains all of the logic for finding pages to transition to and from, and how to handle various response conditions such as a page not found. <code>$.mobile.changePage()</code> can be called externally and accepts the following arguments (to, transition, back, changeHash). The <code>to</code> argument can accept either a string (such as a file url or local element's ID), an array (in which the first array item is any local page you'd like to transition from, and the second array item is the <code>to</code> page), or an object (with expected properties: url, type ("get" or "post"), and data (for serialized parameters)), the latter of which is useful for loading pages that expect form data. The <code>transition</code> argument accepts a string representing a named transition, such as "slide". The <code>back</code> argument accepts a boolean representing whether the transition should go forward or in reverse. Lastly, the <code>changeHash</code> argument accepts a boolean for whether you'd like the url to be updated upon a successful page change.</p>
  37. <p>The <code>$.mobile.changePage()</code> function is used in a number of places in jQuery Mobile. For example, when a link is clicked, its href attribute is normalized and then <code>$.mobile.changePage()</code> handles the rest. When forms are submitted, jQuery Mobile simply gathers a few of the form's attributes, serializes its data, and once again, <code>$.mobile.changePage()</code> is used to handle the submission and response. Also, links that create dialogs use <code>$.mobile.changePage()</code>to open a referenced page without updating the hash, which is useful for keeping dialogs out of history tracking. </p>
  38. <h2>Base element</h2>
  39. <p>Another key ingredient to jQuery Mobile's page navigation model is the <code>base</code> element, which is injected into the <code>head</code> and modified on every page change to ensure that any assets (css,images,js,etc) referenced on that page will be requested from a proper path. In browsers that don't support dynamic updates to the <code>base</code> element (such as Firefox 3.6), jQuery Mobile loops through all of the referenced assets on the page and prefixes their href and src attributes with the base path.</p>
  40. <h2>Developer explanation of base url management:</h2>
  41. <p>jQuery Mobile manages http requests using a combination of generated absolute URL paths and manipulating a generated <code>&lt;base&gt;</code> element's href attribute. The combination of these two approaches allows us to create URLs that contain full path information for loading pages, and a base element to properly direct asset requests made by those loaded pages (such as images and stylesheets).</p>
  42. <p><strong>TODO: update description of internal base and urlHistory objects</strong></p>
  43. <h2>Auto-generated pages and sub-hash urls</h2>
  44. <p>Some plugins may choose to dynamically break a page's content into separate navigable pages, which can then be reached via deep links. One example of this would be the Listview plugin, which will break a nested UL (or OL) into separate pages, which are each given a data-url attribute so they can be linked to like any normal "page" in jQuery Mobile. However, in order to link to these pages, the page that generates them must first be requested from the server. To make this work, pages that are auto-generated by plugins use the following special data-url structure:
  45. <code>&lt;div data-url="page.html&amp;subpageidentifier"&gt;</code></p>
  46. <p>So, for example, a page generated by the listview plugin may have an data-url attribute like this: <code>data-url="artists.html&amp;ui-page=listview-1"</code></p>
  47. <p>When a page is requested, jQuery Mobile knows to split the URL at "&amp;ui-page" and make an HTTP request to the portion of the URL before that key. In the case of the listview example mentioned above, the URL would look like this:;ui-page=listview-1
  48. ...and jQuery Mobile would request artists.html, which would then generate its sub-pages, creating the div with data-url="artists.html&amp;ui-page=listview-1", which it will then display as the active page.</p>
  49. <p><em>Note that the data-url attribute of the element contains the full URL path, not just the portion after &amp;ui-page=. This allows jQuery Mobile to use a single consistent mechanism that matches URLs to page data-url attributes.</em></p>
  50. <h2>Cases when Ajax navigation will not be used</h2>
  51. <p>Under certain conditions, normal http requests will be used instead of Ajax requests. One case where this is true is when linking to pages on external websites. You can also specify that a normal http request be made through the following link attributes:</p>
  52. <ul>
  53. <li><p>rel=external</p></li>
  54. <li><p>target (with any value, such as "_blank")</p></li>
  55. </ul><h2>Form submissions</h2>
  56. <p>Form submissions are handled automatically through the navigation model as well. Visit the forms section for more information.</p>
  57. <h2>Known limitations</h2>
  58. <p>The non-standard environment created by jQuery Mobile's page navigation model introduces some conditions for which you should be aware when building pages:</p>
  59. <ul>
  60. <li><p>When linking to directories, without a filename url, (such as href="typesofcats/" instead of href="typesofcats/index.html"), you must provide a trailing slash. This is because jQuery Mobile assumes the section after the last "/" character in a url is a filename, and it will remove that section when creating base urls from which future pages will be referenced.</p></li>
  61. <li><p>Documents loaded via Ajax will select the first page in the DOM of that document to be loaded as a JQM page element. As a result the developer must make sure to manage the ID attributes of the loaded page and child elements to prevent confusion when manipulating the DOM.</p></li>
  62. <li><p>Any unique assets referenced by pages in a jQuery Mobile-driven site should be placed inside the "page" element (the element with a data-role attribute of "page"). For example, links to styles and scripts that are specific to a particular page can be referenced inside that div. However, a better approach is to use jQuery Mobile's page events to trigger specific scripting when certain pages load. <strong>Note: </strong> you can return a page from the server with a data-url already specified in the markup, and jQuery Mobile will use that for the hash update. This allows you to ensure directory paths resolve with a trailing slash and will therefore be used in the base url path for future requests.</p></li>
  63. <li><p>Conversely, any non-unique assets (those used site-wide) should be referenced in the <code>&lt;head&gt;</code> section of an HTML document, or at the very least, outside of the "page" element, to prevent running scripts more than once.</p></li>
  64. <li><p>The "ui-page" key name used in sub-hash url references can be set to any value you'd like, so as to blend into your URL structure. This value is stored in <code></code>.</p></li>
  65. </ul>
  66. </div><!--/content-primary -->
  67. <div class="content-secondary">
  68. <div data-role="collapsible" data-collapsed="true" data-theme="b">
  69. <h3>More in this section</h3>
  70. <ul data-role="listview" data-theme="c" data-dividertheme="d">
  71. <li data-role="list-divider">Pages &amp; Dialogs</li>
  72. <li><a href="page-anatomy.html">Anatomy of a page</a></li>
  73. <li><a href="page-template.html" data-ajax="false">Single page template</a></li>
  74. <li><a href="multipage-template.html" data-ajax="false">Multi-page template</a></li>
  75. <li><a href="page-titles.html">Page titles</a></li>
  76. <li><a href="page-links.html">Linking pages</a></li>
  77. <li><a href="page-transitions.html" data-ajax="false">Page transitions</a></li>
  78. <li><a href="page-dialogs.html">Dialogs</a></li>
  79. <li><a href="page-cache.html">Prefetching &amp; caching pages</a></li>
  80. <li data-theme="a"><a href="page-navmodel.html">Ajax, hashes &amp; history</a></li>
  81. <li><a href="page-dynamic.html">Dynamically Injecting Pages</a></li>
  82. <li><a href="page-scripting.html">Scripting pages</a></li>
  83. <li><a href="pages-themes.html">Theming pages</a></li>
  84. </ul>
  85. </div>
  86. </div>
  87. </div><!-- /content -->
  88. <div data-role="footer" class="footer-docs" data-theme="c">
  89. <p>&copy; 2011 The jQuery Project</p>
  90. </div>
  91. </div><!-- /page -->
  92. </body>
  93. </html>