1

我正在编写一个为浏览器提供 JavaScript 应用程序的 Web 应用程序平台。不用说,我在加载文档后使用 JS 方法启动了一个应用程序,但在 IE9 上,如果开发者控制台没有被摆弄,什么都不会发生。

这似乎是典型的缺少控制台问题,但我无法通过添加控制台检查或从源代码中删除控制台调用来解决它。

你们能看出我哪里出错了吗?

我正在使用同一平台为多个单独的 Web 应用程序提供服务,因此您还可以查看以下内容(问题似乎完全一样):

4

1 回答 1

1

并非所有版本的 Internet Explorer 都支持 Object.keys:请参考以下内容:https ://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys 以下(来自上述来源)添加Object.keys 到不支持它的浏览器:


if (!Object.keys) {
  Object.keys = (function () {
    var hasOwnProperty = Object.prototype.hasOwnProperty,
        hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
        dontEnums = [
          'toString',
          'toLocaleString',
          'valueOf',
          'hasOwnProperty',
          'isPrototypeOf',
          'propertyIsEnumerable',
          'constructor'
        ],
        dontEnumsLength = dontEnums.length

    return function (obj) {
      if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null) throw new TypeError('Object.keys called on non-object')

      var result = []

      for (var prop in obj) {
        if (hasOwnProperty.call(obj, prop)) result.push(prop)
      }

      if (hasDontEnumBug) {
        for (var i=0; i 

In addition, your method of checking the existence of console is erroneous :

Try running (http://jsfiddle.net/PytAh/) in internet explorer:

if (console){
    alert("there");
} else {
    alert("not there");
}

It will generate an error showing that console does not exist. You can replace it by :

if (window.console){
    alert("there");
} else {
    alert("not there");
}

于 2012-07-12T07:22:47.980 回答