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

Commit

Permalink
Fix hashchange event on IE8 compatibility mode
Browse files Browse the repository at this point in the history
Stupid IE8 in compatibility mode or in IE7 mode returns true for `('onhashchange' in window)`, but does not support hashchange event.

Closes #353
  • Loading branch information
vojtajina authored and IgorMinar committed Jun 2, 2011
1 parent aa64d37 commit 50076b5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
18 changes: 18 additions & 0 deletions regression/issue-353.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<html xmlns:ng="http://angularjs.org">
<script type="text/javascript" src="../build/angular.js" ng:autobind></script>
<script type="text/javascript">
function Cntl($route) {
$route.when('/item1', {});
$route.when('/item2', {});
$route.onChange(function() {
alert('change');
});
}
Cntl.$inject = ['$route'];
</script>
<body ng:controller="Cntl">
<a href="#/item1">test</a>
<a href="#/item2">test</a>
</body>
</html>
4 changes: 3 additions & 1 deletion src/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ function Browser(window, document, body, XHR, $log) {
* @return {function()} Returns the registered listener fn - handy if the fn is anonymous.
*/
self.onHashChange = function(listener) {
if ('onhashchange' in window) {
// IE8 comp mode returns true, but doesn't support hashchange event
var dm = window.document.documentMode;
if ('onhashchange' in window && (isUndefined(dm) || dm >= 8)) {
jqLite(window).bind('hashchange', listener);
} else {
var lastBrowserUrl = self.getUrl();
Expand Down
27 changes: 25 additions & 2 deletions test/BrowserSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ describe('browser', function(){
it('should use $browser poller to detect url changes when onhashchange event is unsupported',
function() {

fakeWindow = {location: {href:"http://server"}};
fakeWindow = {
location: {href:"http://server"},
document: {}
};

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

Expand Down Expand Up @@ -455,7 +458,8 @@ describe('browser', function(){
onHashChngListener = listener;
},
removeEventListener: angular.noop,
detachEvent: angular.noop
detachEvent: angular.noop,
document: {}
};
fakeWindow.onhashchange = true;

Expand All @@ -479,5 +483,24 @@ describe('browser', function(){
jqLite(fakeWindow).dealoc();
}
});

// asynchronous test
it('should fire onHashChange when location.hash change', function() {
var callback = jasmine.createSpy('onHashChange');
browser = new Browser(window, {}, {});
browser.onHashChange(callback);

window.location.hash = 'new-hash';
browser.startPoller(100, setTimeout);

waitsFor(function() {
return callback.callCount;
}, 'onHashChange callback to be called', 1000);

runs(function() {
if (!jQuery) jqLite(window).dealoc();
window.location.hash = '';
});
});
});
});

0 comments on commit 50076b5

Please sign in to comment.