Skip to content

Commit 05671a8

Browse files
sirughbenvinegar
authored andcommitted
handle/test scenario where addEventListener doesnt exist (#595)
1 parent de52931 commit 05671a8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/raven.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,15 @@ Raven.prototype = {
813813
// Capture breadcrubms from any click that is unhandled / bubbled up all the way
814814
// to the document. Do this before we instrument addEventListener.
815815
if (this._hasDocument) {
816-
document.addEventListener('click', self._breadcrumbEventHandler('click'));
817-
document.addEventListener('keypress', self._keypressEventHandler());
816+
if (document.addEventListener) {
817+
document.addEventListener('click', self._breadcrumbEventHandler('click'));
818+
document.addEventListener('keypress', self._keypressEventHandler());
819+
}
820+
else {
821+
// IE8 Compatibility
822+
document.attachEvent('onclick', self._breadcrumbEventHandler('click'));
823+
document.attachEvent('onkeypress', self._keypressEventHandler());
824+
}
818825
}
819826

820827
// event targets borrowed from bugsnag-js:

test/raven.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,23 @@ describe('install/uninstall', function () {
21302130
Raven.install();
21312131
assert.isTrue(TraceKit.report.subscribe.calledOnce);
21322132
});
2133+
2134+
it('should use attachEvent instead of addEventListener in IE8', function () {
2135+
// Maintain a ref to the old function so we can restore it later.
2136+
var temp = document.addEventListener;
2137+
2138+
// Test setup.
2139+
this.sinon.stub(Raven, 'isSetup').returns(true);
2140+
document.addEventListener = false;
2141+
document.attachEvent = this.sinon.stub();
2142+
2143+
// Invoke and assert.
2144+
Raven.install();
2145+
assert.isTrue(document.attachEvent.called);
2146+
2147+
// Cleanup.
2148+
document.addEventListener = temp;
2149+
});
21332150
});
21342151

21352152
describe('.uninstall', function() {

0 commit comments

Comments
 (0)