Home > Web Front-end > JS Tutorial > jQuery Mobile: When to Use `pageinit` vs. `$(document).ready()`?

jQuery Mobile: When to Use `pageinit` vs. `$(document).ready()`?

Barbara Streisand
Release: 2024-12-17 05:01:26
Original
521 people have browsed it

jQuery Mobile:  When to Use `pageinit` vs. `$(document).ready()`?

jQuery Mobile: Page Events vs. Document Ready

Differences and Usage

While $(document).ready() is traditionally used for DOM element manipulation, it may not be suitable for jQuery Mobile applications due to its Ajax-based page loading. This can lead to code execution before pages are fully loaded.

In jQuery Mobile, page events provide better control over page execution. $(document).on('pageinit') is triggered when a page is first loaded into the DOM and is an ideal place to initialize page-specific functionality.

Benefits of Using Page Events

  • Avoid code execution before pages are loaded.
  • Execute code specific to a particular page.
  • Control the execution order of events during page transitions.

Page Event Order During Transitions

When transitioning between pages, events are triggered in the following order:

  1. pagebeforecreate (page B)
  2. pagecreate (page B)
  3. pageinit (page B)
  4. pagebeforehide (page A)
  5. pageremove (page A)
  6. pagehide (page A)
  7. pagebeforeshow (page B)
  8. pageshow (page B)

Data Manipulation Between Pages

Parameters can be passed from one page to another using the dataUrl and data options in $.mobile.changePage. These parameters can be retrieved using $(this).data("url") or $(this).data("data") in the target page's pagebeforeshow event.

Conclusion

Page events provide more precise control over code execution in jQuery Mobile applications. By leveraging these events, developers can avoid common pitfalls and ensure consistent execution across pages.

The above is the detailed content of jQuery Mobile: When to Use `pageinit` vs. `$(document).ready()`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template