So I ran into this same problem with the URL bar annoyingly dropping in and out between page views. However, I found that removing the HREF attribute on links caused other aesthetic-related issues as well as functional issues where something else was relying on the HREF attribute. Also, I didn't really like the idea of taking away the one attribute which makes a link a link. My solution aims to remove the HREF attribute just long enough for the "click" event to dispatch and then restore it back afterwards. Here is round one of working code:
- var $this,
- $doc = $(document),
- $body = $('body');
- $doc.on('pagechange', function(ev) {
- $.mobile.activePage.find('a[href]').each(function() {
- $this = $(this);
- if ( $this.attr('href') !== '#' && !$this.is( "[rel='external']" ) && !$this.is( ":jqmData(ajax='false')" ) && !$this.is( "[target]" ) ) {
- $this.attr('data-href', $this.attr('href'));
- }
- });
-
- $body.off('vclick.autoHideUrlBar').on('vclick.autoHideUrlBar', 'a[data-href]', function(ev) {
- $(this).removeAttr('href');
- });
-
- $body.off('click.autoHideUrlBar').on('click.autoHideUrlBar', 'a[data-href]', function(ev) {
- $this = $(this);
- $this.attr( 'href', $this.attr('data-href') );
- });
- });
IMPORTANT: jQuery Mobile checks for the "href" attribute throughout it's code and performs different actions based on whether or not there is a value. You will need to go in and modify JQM to also check for the "data-href" as well so that existing functionality does not break. In my app, I only changed the code in 2 places so far.
First, open JQM.js and search for the term "_registerInternalEvents". There are only 2 instances: the definition and the call. We are interested in the definition, which is where JQM registers it's own "vclick" and "click" events. Once you have found the _registerInternalEvents function definition, you can scroll down about ~100 lines of code and find the following line:
- $.mobile.document.bind( "vclick", function() { ...
Scroll down another 70 lines or so and you will find this line
- $.mobile.document.bind( "click", function() { ...
Within BOTH of those click handlers you will see code where JQM checks the HREF attribute:
- target.getAttribute( "href" ) // Used in the "vclick" handler
- $link.attr( "href" ) // Used in the "click" handler
You will want to keep that code in place, but also add checks for the "data-href" attribute so that the code snippets above will look like this when you are done:
- target.getAttribute( "href" ) || target.getAttribute( "data-href" )
- $link.attr( "href" ) || $link.attr( "data-href" )
That's it. The above is working for me on a current project. I will be going though JQM and finding all instances of "href" and updating it to also look for "data-href". Pro tip, I will actually be wrapping this into a "$.fn.getHref()" function or something of the sorts. Cheers.
~Ryan