wrapping event handlers with document.on(pageinit)
I'm writing a simple mobile web site using JQuery Mobile.
I wrote this code to handle clicks on anchors pointing to bookmarks within the page.
I put the code within a function, and I call the function from within the head HTML section.
Here is the code:
- function initPage() {
- // Anchor links handling.
- $(document).on('vclick', 'a[href^=#][href!=#]', function() {
- location.hash = $(this).attr('href');
- return false;
- });
- }
Here is HTML fragment calling the code:
- <html>
- <head>
- ...
- <script type="text/javascript">
- initPage();
- </script>
- ...
My code works fine, but I have a doubt about which is the best way to call the code and whether should I wrap my code with $(document).on('pageinit') ?
Like this?
- function initPage() {
- $(document).on('pageinit', function(){
- // Anchor links handling.
- $(document).on('vclick', 'a[href^=#][href!=#]', function() {
- location.hash = $(this).attr('href');
- return false;
- });
- });
- }
Or maybe like this?
- <html>
- <head>
- ...
- <script type="text/javascript">
- $(document).on('pageinit', function(){
- initPage();
- });
- </script>
- ...
Many thanks for support.