diff --git a/js/jquery.mobile.init.js b/js/jquery.mobile.init.js index 688cff988ec..927ffd7abd0 100644 --- a/js/jquery.mobile.init.js +++ b/js/jquery.mobile.init.js @@ -77,8 +77,10 @@ define( [ "jquery", "./jquery.mobile.core", "./jquery.mobile.support", "./jquery //remove initial build class (only present on first pageshow) hideRenderingClass(); - // if hashchange listening is disabled or there's no hash deeplink, change to the first page in the DOM - if ( !$.mobile.hashListeningEnabled || !$.mobile.path.stripHash( location.hash ) ) { + // if hashchange listening is disabled, there's no hash deeplink, + // the hash is not valid (contains more than one # or does not start with #) + // or there is no page with that hash, change to the first page in the DOM + if ( !$.mobile.hashListeningEnabled || !$.mobile.path.isHashValid( location.hash ) || !$( location.hash + ':jqmData(role="page")' ).length ) { $.mobile.changePage( $.mobile.firstPage, { transition: "none", reverse: true, changeHash: false, fromHashChange: true } ); } // otherwise, trigger a hashchange to load a deeplink diff --git a/js/jquery.mobile.navigation.js b/js/jquery.mobile.navigation.js index 14cbbdcb479..c6403d7050f 100644 --- a/js/jquery.mobile.navigation.js +++ b/js/jquery.mobile.navigation.js @@ -212,6 +212,10 @@ define( [ return path.stripHash( hash.replace( /\?.*$/, "" ).replace( dialogHashKey, "" ) ); }, + isHashValid: function( hash ) { + return !( /(^#$)|(#.*#.*)|(^[^#].+)/ ).test( hash ); + }, + //check whether a url is referencing the same domain, or an external domain or different protocol //could be mailto, etc isExternal: function( url ) { diff --git a/tests/unit/navigation/navigation_helpers.js b/tests/unit/navigation/navigation_helpers.js index 88533b76c78..038f5c52afe 100644 --- a/tests/unit/navigation/navigation_helpers.js +++ b/tests/unit/navigation/navigation_helpers.js @@ -215,4 +215,12 @@ same( $.mobile.path.cleanHash( "#anything/atall?akjfdjjf" ), "anything/atall", "removes query param"); same( $.mobile.path.cleanHash( "#nothing/atall" ), "nothing/atall", "removes query param"); }); -})(jQuery); \ No newline at end of file + + test( "path.isHashValid", function(){ + same( $.mobile.path.isHashValid( "#id" ), true, "Valid hash"); + same( $.mobile.path.isHashValid( "#" ), false, "Empty hash"); + same( $.mobile.path.isHashValid( "#id#" ), false, "Hash with more than one #"); + same( $.mobile.path.isHashValid( "id" ), false, "Hash without #"); + same( $.mobile.path.isHashValid( "i#d" ), false, "Hash with # in the wrong spot"); + }); +})(jQuery);