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

Commit 50076b5

Browse files
vojtajinaIgorMinar
authored andcommitted
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 #353
1 parent aa64d37 commit 50076b5

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

regression/issue-353.html

+18
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

+3-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ function Browser(window, document, body, XHR, $log) {
249249
* @return {function()} Returns the registered listener fn - handy if the fn is anonymous.
250250
*/
251251
self.onHashChange = function(listener) {
252-
if ('onhashchange' in window) {
252+
// IE8 comp mode returns true, but doesn't support hashchange event
253+
var dm = window.document.documentMode;
254+
if ('onhashchange' in window && (isUndefined(dm) || dm >= 8)) {
253255
jqLite(window).bind('hashchange', listener);
254256
} else {
255257
var lastBrowserUrl = self.getUrl();

test/BrowserSpecs.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,10 @@ describe('browser', function(){
418418
it('should use $browser poller to detect url changes when onhashchange event is unsupported',
419419
function() {
420420

421-
fakeWindow = {location: {href:"http://server"}};
421+
fakeWindow = {
422+
location: {href:"http://server"},
423+
document: {}
424+
};
422425

423426
browser = new Browser(fakeWindow, {}, {});
424427

@@ -455,7 +458,8 @@ describe('browser', function(){
455458
onHashChngListener = listener;
456459
},
457460
removeEventListener: angular.noop,
458-
detachEvent: angular.noop
461+
detachEvent: angular.noop,
462+
document: {}
459463
};
460464
fakeWindow.onhashchange = true;
461465

@@ -479,5 +483,24 @@ describe('browser', function(){
479483
jqLite(fakeWindow).dealoc();
480484
}
481485
});
486+
487+
// asynchronous test
488+
it('should fire onHashChange when location.hash change', function() {
489+
var callback = jasmine.createSpy('onHashChange');
490+
browser = new Browser(window, {}, {});
491+
browser.onHashChange(callback);
492+
493+
window.location.hash = 'new-hash';
494+
browser.startPoller(100, setTimeout);
495+
496+
waitsFor(function() {
497+
return callback.callCount;
498+
}, 'onHashChange callback to be called', 1000);
499+
500+
runs(function() {
501+
if (!jQuery) jqLite(window).dealoc();
502+
window.location.hash = '';
503+
});
504+
});
482505
});
483506
});

0 commit comments

Comments
 (0)