Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 3f04770

Browse files
Richard Collinscaitp
Richard Collins
authored andcommitted
fix($location): make legacy browsers behave like modern ones in html5Mode
Previously, LocationHashbangInHtml5Url, which is used when html5Mode is enabled in browsers which do not support the history API (IE8/9), would behave very inconsistently WRT relative URLs always being resolved relative to the app root url. This fix enables these legacy browsers to behave like history enabled browsers, by processing href attributes in order to resolve urls correctly. Closes #6162 Closes #6421 Closes #6899 Closes #6832 Closes #6834
1 parent b2e48e6 commit 3f04770

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/ng/location.js

+42
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ function LocationHashbangInHtml5Url(appBase, hashPrefix) {
268268
return appBaseNoFile;
269269
}
270270
};
271+
272+
this.$$compose = function() {
273+
var search = toKeyValue(this.$$search),
274+
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
275+
276+
this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
277+
// include hashPrefix in $$absUrl when $$url is empty so IE8 & 9 do not reload page because of removal of '#'
278+
this.$$absUrl = appBase + hashPrefix + this.$$url;
279+
};
280+
271281
}
272282

273283

@@ -642,6 +652,38 @@ function $LocationProvider(){
642652
absHref = urlResolve(absHref.animVal).href;
643653
}
644654

655+
// Make relative links work in HTML5 mode for legacy browsers (or at least IE8 & 9)
656+
// The href should be a regular url e.g. /link/somewhere or link/somewhere or ../somewhere or somewhere#anchor or http://example.com/somewhere
657+
if (LocationMode === LocationHashbangInHtml5Url) {
658+
// get the actual href attribute - see http://msdn.microsoft.com/en-us/library/ie/dd347148(v=vs.85).aspx
659+
// TODO check browser is in standards mode
660+
var href = elm[0].getAttribute('href');
661+
662+
if (href.indexOf('://' == -1)) { // Ignore absolute URLs
663+
if (href[0] == '/') {
664+
// absolute path - replace old path
665+
absHref = serverBase(absHref) + href;
666+
} else if (href[0] == '#') {
667+
// local anchor
668+
absHref = serverBase(absHref) + $location.path() + href;
669+
} else {
670+
// relative path - join with current path
671+
var stack = $location.path().split("/"),
672+
parts = href.split("/");
673+
stack.pop(); // remove top file
674+
for (var i=0; i<parts.length; i++) {
675+
if (parts[i] == ".")
676+
continue;
677+
else if (parts[i] == "..")
678+
stack.pop();
679+
else
680+
stack.push(parts[i]);
681+
}
682+
absHref = serverBase(absHref) + stack.join("/");
683+
}
684+
}
685+
}
686+
645687
var rewrittenUrl = $location.$$rewrite(absHref);
646688

647689
if (absHref && !elm.attr('target') && rewrittenUrl && !event.isDefaultPrevented()) {

0 commit comments

Comments
 (0)