Permalink
Browse files

Fix for issue #2644 - Navigating to root "/" does not generate clean …

…URL if root page has id="*"

- Fixed a bug in the hashchange handler for the pushstate/replacestate plugin that was incorrectly resolving hashchanges for ids against the current location.href, which could be a different document. We now resolve id hashes against the document URL.

- Modified changePage() so that it sets the settings.dataUrl option to the documentUrl, when navigating to the first-page of the application document. This prevents any id on the first-page from being added to the location hash. This means that URLs that used to be produced like this:

	http://site.com/apps/#first-page-id

will now display as:

		http://site.com/apps/

Developers that wish to get the old behavior back can register a pagebeforechange handler and do something like this:

	$( document ).bind( "pagebeforechange", function( e, data ) {
		var toPage = data.toPage;
		if ( typeof toPage === "object" && !data.options.dataUrl && toPage[ 0 ] === $.mobile.firstPage[ 0 ] && toPage[ 0 ].id ) {
			data.options.dataUrl = "#" + toPage[ 0 ].id;
		}
	});

The handler above will make sure that any page changes to the first-page will always display as:

	http://site.com/apps/#first-page-id
  • Loading branch information...
jblas committed Oct 10, 2011
1 parent a589da3 commit c297f9675b08246393eb649ccf2831d426b2b6bf
Showing with 11 additions and 2 deletions.
  1. +8 −0 js/jquery.mobile.navigation.js
  2. +3 −2 js/jquery.mobile.navigation.pushstate.js
@@ -1017,6 +1017,14 @@
return;
}
+ // If we are going to the first-page of the application, we need to make
+ // sure settings.dataUrl is set to the application document url. This allows
+ // us to avoid generating a document url with an id hash in the case where the
+ // first-page of the document has an id attribute specified.
+ if ( toPage[ 0 ] === $.mobile.firstPage[ 0 ] && !settings.dataUrl ) {
+ settings.dataUrl = documentUrl.hrefNoHash;
+ }
+
// The caller passed us a real page DOM element. Update our
// internal state and then trigger a transition to the page.
var fromPage = settings.fromPage,
@@ -65,14 +65,15 @@
var href, state,
hash = location.hash,
- isPath = $.mobile.path.isPath( hash );
+ isPath = $.mobile.path.isPath( hash ),
+ resolutionUrl = isPath ? location.href : $.mobile.getDocumentUrl();
hash = isPath ? hash.replace( "#", "" ) : hash;
// propulate the hash when its not available
state = self.state();
// make the hash abolute with the current href
- href = $.mobile.path.makeUrlAbsolute( hash, location.href );
+ href = $.mobile.path.makeUrlAbsolute( hash, resolutionUrl );
if ( isPath ) {
href = self.resetUIKeys( href );

0 comments on commit c297f96

Please sign in to comment.