问题
In my MVC4 application I have some code to push a new entry in the eventlog to a webpage with SignalR. This code works in FireFox, but in IE8 when a new event is added to the event log the debugger pops up with Error: Object doesn't support this property or method
and stops at the following code:
this.notifyListeners = function (queryName, evt) {
registeredListeners.forEach(function (item) {
if (item.queryName === queryName) {
item.listener.newEvent(evt);
}
});
};
When I step over this method the debugger closes and the event is shown on the page. Why does this happen and how can I workaround this?
回答1:
forEach
is not supported in IE8 you need to polyfill
Like suggested here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisArg */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++)
{
if (i in t)
fun.call(thisArg, t[i], i, t);
}
};
}
Or perform and standard for
loop, something like:
this.notifyListeners = function (queryName, evt) {
var item={};
for (i=0;i<registeredListeners.length;i++){
item = registeredListeners[i];
if (item.queryName === queryName) {
item.listener.newEvent(evt);
}
}
};
来源:https://stackoverflow.com/questions/21312537/pushing-updates-from-event-log-to-webpage-not-working-in-ie8