Home >Web Front-end >JS Tutorial >jQuery Mobile: When Should I Use `pageinit` Over `$(document).ready()`?
jQuery Mobile introduced page events to address a limitation of $(document).ready(), which may trigger before mobile pages are loaded and can lead to issues. Page events, such as pageinit, provide a more fine-grained way to execute code related to specific pages.
When transitioning from one page to another, the order of page events is:
Unload Events
Load Events
Show/Hide Events
To send data between pages, use the data or url property of the $.mobile.changePage() function. To access the data on the receiving page, use the $(this).data() or $(this).attr("data-url") methods.
For better code organization, it's recommended to keep page initialization code separate in a JavaScript file. For global settings, use the mobileinit event.
To prevent page transitions based on certain conditions, use the pagebeforechange event and the e.preventDefault() method.
To prevent multiple event bindings, use pageinit for event handling or remove event handlers before rebinding them.
Page transitions involve loading, enhancing (styling), and executing transitions. The average execution time for a transition is around 670 milliseconds.
Here are some methods for manipulating data between pages:
The above is the detailed content of jQuery Mobile: When Should I Use `pageinit` Over `$(document).ready()`?. For more information, please follow other related articles on the PHP Chinese website!