Skip to content

Commit 72d2807

Browse files
committed
Fix hashchange event on IE8 compatibility mode
Stupid IE8 in compatibility mode or in IE7 mode returns true for `('onhashchange' in window)`, but does not support hashchange event. Closes angular#353
1 parent 805e083 commit 72d2807

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

regression/issue-353.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:ng="http://angularjs.org">
3+
<script type="text/javascript" src="../build/angular.js" ng:autobind></script>
4+
<script type="text/javascript">
5+
function Cntl($route) {
6+
$route.when('/item1', {});
7+
$route.when('/item2', {});
8+
$route.onChange(function() {
9+
alert('change');
10+
});
11+
}
12+
Cntl.$inject = ['$route'];
13+
</script>
14+
<body ng:controller="Cntl">
15+
<a href="#/item1">test</a>
16+
<a href="#/item2">test</a>
17+
</body>
18+
</html>

src/Browser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ function Browser(window, document, body, XHR, $log) {
247247
* @return {function()} Returns the registered listener fn - handy if the fn is anonymous.
248248
*/
249249
self.onHashChange = function(listener) {
250-
if ('onhashchange' in window) {
250+
// IE8 comp mode returns true, but doesn't support hashchange event
251+
var dm = window.document.documentMode;
252+
if ('onhashchange' in window && (!isDefined(dm) || dm >= 8)) {
251253
jqLite(window).bind('hashchange', listener);
252254
} else {
253255
var lastBrowserUrl = self.getUrl();

test/BrowserSpecs.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,10 @@ describe('browser', function(){
405405
it('should use $browser poller to detect url changes when onhashchange event is unsupported',
406406
function() {
407407

408-
fakeWindow = {location: {href:"http://server"}};
408+
fakeWindow = {
409+
location: {href:"http://server"},
410+
document: {}
411+
};
409412

410413
browser = new Browser(fakeWindow, {}, {});
411414

@@ -442,7 +445,8 @@ describe('browser', function(){
442445
onHashChngListener = listener;
443446
},
444447
removeEventListener: angular.noop,
445-
detachEvent: angular.noop
448+
detachEvent: angular.noop,
449+
document: {}
446450
};
447451
fakeWindow.onhashchange = true;
448452

@@ -466,5 +470,24 @@ describe('browser', function(){
466470
jqLite(fakeWindow).dealoc();
467471
}
468472
});
473+
474+
// asynchronous test
475+
it('should fire onHashChange when location.hash change', function() {
476+
var callback = jasmine.createSpy('onHashChange');
477+
browser = new Browser(window, {}, {});
478+
browser.onHashChange(callback);
479+
480+
window.location.hash = 'new-hash';
481+
browser.startPoller(100, setTimeout);
482+
483+
waitsFor(function() {
484+
return callback.callCount;
485+
}, 'onHashChange callback to be called', 1000);
486+
487+
runs(function() {
488+
if (!jQuery) jqLite(window).dealoc();
489+
window.location.hash = '';
490+
});
491+
});
469492
});
470493
});

0 commit comments

Comments
 (0)