Skip to content
This repository

Bad URLs with hashes are not gracefully handled #4119

Closed
jlembeck opened this Issue April 19, 2012 · 5 comments

7 participants

Jeff Lembeck John Bender Ghislain Seguin Jason Scott Todd Parker Barney Carroll Sven Franck
Jeff Lembeck

I'm not sure if this is by design or not, but

http://jquerymobile.com/demos/1.1.0/##

Is a decent example of this.

I'd expect the page to just load at this point, instead it freezes with a JS error. This is likely caused by the way that hash changes are handled.

Todd Parker

Yeah, this is a bug for sure. In fact, an empty hash should probably be ignored too (/#), right now it navigate you to the root.

Barney Carroll

Double hash, or any form of bad URL, is not necessary — any (legitimate) hash will break init, eg:

http://jquerymobile.com/demos/1.1.0/#jqm-logo

From my execution, no errors are thrown, and no custom events are fired — the page rests in a loading state and initialisation is frozen.

EDIT: You can prevent this behaviour by executing jQuery.mobile.hashListeningEnabled = false; before DOM ready. But you shouldn't have to chose between a staple of native web behaviour & jQM history management.

Sven Franck

I'm also running across this when trying to load deeplinks using my multiview plugin, because by default my some.html#nested_page_ID will also be a bad URL. I have patched my JQM like this to fix it for me:

 // also check for panel subpages
 if ( !$.mobile.hashListeningEnabled || !$.mobile.path.stripHash( location.hash ) || $( window.location.hash ).closest('div:jqmData(role="panel")').length > 0 ) {              
            // store hash, retrieve inside multiview
            $('html').data("deep", window.location.hash);
            // back to JQM
            $.mobile.changePage( $.mobile.firstPage, { transition: "none", reverse: true, changeHash: false, fromHashChange: true } );
        }

I'm trying to get by without touching JQM.js, so in case you are adding some more logic and are open for wishes... I would also like a custom event such as "loadFirstPage" to tap into.

I guess once you start thinking about supporting anchor tags, you will need some more logic here anyway, because all anchor tags will be bad as of now. What do you think?

Sven Franck

@gseguin

Thinking more about a loadFirstPage event.... this would also be very useful for deeplinks to artifical hashtags. Say I have a page some.html, in which I'm updating content via Ajax.

I would like to reflect the updated page by changing the hashtag, but I can't because all deeplinks to this page ala some.html#new_content will break, because the page isn't there and I'm missing an event to tap into the first page being loaded to redirect to the actual page and fire an Ajax call to update the content.

No problem on the 2nd page with pagebeforechange, but on the first page, this does not fire, does it?

If I could do:

$(document).on('firstpageload', ':jqmData(role="page")', function(e, data) {
    if ( if the deeplink requested is NOT in the document ) {
        e.preventDefault();
        // do something else
        } 
    })
Jason Scott jasondscott referenced this issue from a commit May 25, 2012
Commit has since been removed from the repository and is no longer available.
Jeff Lembeck

Just to note, based on this proposed fix: when hash listening is enabled, things still break.

Jason Scott jasondscott closed this issue from a commit May 25, 2012
Jason Scott Fixes #4119 - Bad URLs with hashes are not gracefully handled
Fixes issue #4119
Added regex test to see if hash is valid and if a page with that id
exists, if not load the first page.
fb0a766
Jason Scott jasondscott closed this in fb0a766 May 25, 2012
Jason Scott jasondscott referenced this issue from a commit May 25, 2012
Jason Scott Fixes #4119 - Bad URLs with hashes are not gracefully handled
Fixes issue #4119
Added regex test to see if hash is valid and if a page with that id
exists, if not load the first page.

Added unit tests for isHashValid
b43ec24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.