From 4b4ee54a72493972b55cf559a88d0c290375230b Mon Sep 17 00:00:00 2001 From: scottjehl Date: Fri, 6 May 2011 09:37:41 -0400 Subject: [PATCH] This change sets the base tag properly on page load and page changes, corresponding with the recent change to absolute path hash urls. Images and other assets will direct relative to the document in which they reside. This may not cover new path situations Kin has documented, but it does fix the regression so things work as they did before the change. Tested in non-base supporting browsers to verify it works there too. The following page should have an image in the content, verifying the fix http://jquerymobile.com/test/#/test/docs/about/intro.html . Fixes #1508 --- js/jquery.mobile.navigation.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/js/jquery.mobile.navigation.js b/js/jquery.mobile.navigation.js index 24eaf513..c5e30f1d 100644 --- a/js/jquery.mobile.navigation.js +++ b/js/jquery.mobile.navigation.js @@ -201,33 +201,28 @@ //existing base tag? $base = $head.children( "base" ), - hostURL = location.protocol + "//" + location.host, - docLocation = path.get( hostURL + location.pathname ), - docBase = docLocation; - + + //get domain path + //(note: use explicit protocol here, protocol-relative urls won't work as expected on localhost) + docBase = location.protocol + "//" + location.host, + + //initialPath for first page load without hash. pathname (href - search) + initialPath = docBase + location.pathname; + + //already a base element? if ( $base.length ) { var href = $base.attr( "href" ); if ( href ) { if ( href.search( /^[^:\/]+:\/\/[^\/]+\/?/ ) === -1 ) { //the href is not absolute, we need to turn it into one - //so that we can turn paths stored in our location hash into - //relative paths. - if ( href.charAt( 0 ) === "/" ) { - //site relative url - docBase = hostURL + href; - } - else { - //the href is a document relative url - docBase = docLocation + href; - //XXX: we need some code here to calculate the final path - // just in case the docBase contains up-level (../) references. - } + docBase = docBase + href; } else { //the href is an absolute url docBase = href; } } + //make sure docBase ends with a slash docBase = docBase + ( docBase.charAt( docBase.length - 1 ) === "/" ? " " : "/" ); } @@ -236,7 +231,7 @@ var base = $.support.dynamicBaseTag ? { //define base element, for use in routing asset urls that are referenced in Ajax-requested markup - element: ( $base.length ? $base : $( "", { href: docBase } ).prependTo( $head ) ), + element: ( $base.length ? $base : $( "", { href: initialPath } ).prependTo( $head ) ), //set the generated BASE element's href attribute to a new page's base path set: function( href ) {