"SCRIPT16389: Unspecified error" from AngularJs in IE if no browser history #10367
Description
Angular version: 1.3.4
Browser version: Internet Explorer 10
In IE10 (may also occur in other versions of IE as well but haven't got easy access to other versions to test) then if a web application using AngularJs is the very first page loaded in the browser the following JavaScript error may occur:
SCRIPT16389: Unspecified error.
angular.js, line 4116 character 11
This is coming from the call to factory() in the following:
function getService(serviceName) {
if (cache.hasOwnProperty(serviceName)) {
if (cache[serviceName] === INSTANTIATING) {
throw $injectorMinErr('cdep', 'Circular dependency found: {0}',
serviceName + ' <- ' + path.join(' <- '));
}
return cache[serviceName];
} else {
try {
path.unshift(serviceName);
cache[serviceName] = INSTANTIATING;
return cache[serviceName] = factory(serviceName);
} catch (err) {
if (cache[serviceName] === INSTANTIATING) {
delete cache[serviceName];
}
throw err;
} finally {
path.shift();
}
}
}
After further investigation I've found that this appears to be originating from the "cachedState = window.history.state;" statement in the following function.
function cacheState() {
// This should be the only place in $browser where `history.state` is read.
cachedState = window.history.state;
cachedState = isUndefined(cachedState) ? null : cachedState;
// Prevent callbacks fo fire twice if both hashchange & popstate were fired.
if (equals(cachedState, lastCachedState)) {
cachedState = lastCachedState;
}
lastCachedState = cachedState;
}
This error does not occur if any other page has been visited prior to going to the page that uses AngularJs as window.history.state then no longer returns an error in IE.
Note that this error does not appear to occur when using Firefox or Chrome (as neither of these browsers raise an error when accessing window.history.state).
A try catch block on the window.history.state should fix this:
function cacheState() {
// This should be the only place in $browser where `history.state` is read.
try {
cachedState = window.history.state;
}
catch (e) {
cachedState = null;
}
cachedState = isUndefined(cachedState) ? null : cachedState;
// Prevent callbacks fo fire twice if both hashchange & popstate were fired.
if (equals(cachedState, lastCachedState)) {
cachedState = lastCachedState;
}
lastCachedState = cachedState;
}