Skip to content Skip to sidebar Skip to footer

How To Setup Jquery Mobile Navigation Using Onclick Events

I am building a hybrid application out of Jquery Mobile, Cordova and WordPress. My current question is regarding my navigation between 'pages' in index.html that have the data-role

Solution 1:

Disclaimer: these are a few of what I think of as "best practices." Others may disagree; YMMV. Also this is assuming you don't want to use libraries or frameworks like Vue.js or React.js, which in general will do things quite differently. Depending on circumstances these libraries can have both advantages and drawbacks.

But within those limits, the general idea is this:

  • Keep the event handler generic, so that one function can do multiple things.
  • Pass in stuff that differs between links as attributes. This keeps things related to the activity together at the link.
  • I like to attach the event listener higher up in the DOM and then handle the events as they bubble. In this case we're attaching the event to the ul tag, and catching any click events that bubble up from a tags. IMHO this has a few advantages:
    • if you mutate the list, new links will automatically use the current event handler.
    • you only have one event handler attached to the DOM, instead of 3 (however many a tags you have)
    • this also gives you the chance to add other event listeners directly to specific a tags if you want to do something special before (or instead of) the default action. Because events attached directly happen first, and then the event bubbles. If you want it to happen instead of, you would just call e.stopPropagation() to prevent the event from bubbling.

Also what I've done sometimes in the past is to have a single generic page with header and navbar, and then load the main content div via ajax. This has the very visually pleasing effect that when you go to a different page the navbar stays put, and doesn't reload. You could easily do this in the example code below, if changePage was doing an XHR/fetch, and then loading the contents into a main content div.

In this greatly simplified example, I show how we can use the href, innerText, and a data attribute to do different things depending on which link is clicked. Of course you can do as much (or as little) as you want/need in this regard.

$('ul.navbar').on('click', 'a', function(e) {
  var t = e.target;
  var info = t.dataset.info || '';
  console.log("click " + t.innerText + ' ' + info);
  $.mobile.changePage(t.href, {
    transition: "slide"
  });
  e.preventDefault();
  returnfalse;
});

// stub of $.mobile.changePage
$.mobile = {
  changePage: function(href, opts) {
    console.log('changePage', href);
  }
};
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><ulclass='navbar'><li><ahref="#home"data-info="let's go home!">HOME</a></li><li><ahref="#artist">ARTIST</a></li><li><ahref="#info">SHOW INFO</a></li></ul>

Post a Comment for "How To Setup Jquery Mobile Navigation Using Onclick Events"