简体   繁体   English

IE8在使用prototype.js时出错“无效参数”,如何找到错误的位置?

[英]IE8 gives error “Invalid argument” when using prototype.js, how do I find where the error is?

I have a fairly complex piece of Javascript that works flawlessly with no errors in Google Chrome, Firefox, Safari, and Opera. 我有一个相当复杂的Javascript,在Google Chrome,Firefox,Safari和Opera中没有任何错误,可以完美运行。 However, as tends to always be the endlessly annoying case, it completely fails in Internet Explorer. 但是,由于往往总是令人讨厌的情况,它在Internet Explorer中完全失败。 I have tested in IE7 and IE8 and get the same error: 我已经在IE7和IE8中测试过并得到了同样的错误:

Invalid argument. 无效的论点。 prototype.js, line 2216, character 9 prototype.js,第2216行,第9个字符

I am using Prototype 1.6.1 hosted through Google. 我正在使用通过Google托管的Prototype 1.6.1。 The error given isn't very helpful since it doesn't tell me where in my actual code the error is occurring. 给出的错误不是很有帮助,因为它没有告诉我在实际代码中发生错误的位置。 The line mentioned in the error is the 6th line from the bottom in the following code: 错误中提到的行是以下代码中底部的第6行:

setStyle: function(element, styles) {
    element = $(element);
    var elementStyle = element.style, match;
    if (Object.isString(styles)) {
      element.style.cssText += ';' + styles;
      return styles.include('opacity') ?
        element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element;
    }
    for (var property in styles)
      if (property == 'opacity') element.setOpacity(styles[property]);
      else
        elementStyle[(property == 'float' || property == 'cssFloat') ?
          (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') :
            property] = styles[property];

    return element;
  },

Since it is in the setStyle block of code, I assume the error occurs when I am setting style attributes for some element. 因为它在setStyle代码块中,所以我假设当我为某个元素设置样式属性时会发生错误。 However, I call setStyle over 100 times in this script and have been trying to figure out where exactly the error is occurring for several hours. 但是,我在这个脚本中调用了setStyle超过100次,并且一直试图弄清楚错误发生在几个小时的确切位置。 Is there anything I can do to help myself in finding where the error is occurring? 我能做些什么来帮助自己找到发生错误的地方吗?

Put an explicit try ... catch around the code: 明确try ... catch代码:

for (var property in styles) {
  try {
    if (property == 'opacity') element.setOpacity(styles[property]);
    else
      elementStyle[(property == 'float' || property == 'cssFloat') ?
        (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') :
          property] = styles[property];
  }
  catch (_) {
    throw "Error setting property '" + property + "' to value '" + styles[property] + "'";
  }
}

Then you'll know exactly what property and value is causing the problem. 然后你就会确切地知道导致问题的属性和价值。

In IE8 enable the developer tool to break on error [5th button on the script tab.] Click the Start Debugging button. 在IE8中,启用开发人员工具以解决错误[脚本选项卡上的第5个按钮。]单击“ 开始调试”按钮。

When the error occurs, you should be able to inspect the varaibles and see what is causing the problem exactly. 发生错误时,您应该能够检查变量并查看导致问题的原因。

The problem is caused by setStyle({someProperty: null}) . 问题是由setStyle({someProperty: null}) Maybe also undefined or something kind of. 也许也是undefined或某种东西。

To investigate such problems in future, inspect arguments you give to third-party functions in catch block. 要在将来调查此类问题,请检查您在catch块中为第三方函数提供的参数。 Kind of 的种类

try{
  element.setStyle({someProperty: someValue})
}catch(error){
  throw('' + someProperty + ':' + someValue)
}

This code would have pointed you to the source of the error in zero time. 此代码会指向零时间内的错误源。 Here is more detailed snippet for debugging this case using some Prototype.js' helpers: 以下是使用Prototype.js帮助程序调试此案例的更详细的代码段:

;(function () {
  var superFunction = Element.setStyle
  Element.addMethods({
    setStyle: function (element, _arguments) {
      try{
        superFunction(element, _arguments)
      }catch(error){
        console.log(error, $H(_arguments).inspect())
      }
    }
  })
})()

PS in IE8 you should open Developer Tools (F12) to have console working. 在IE8中PS你应该打开开发人员工具(F12)让console工作。

尝试使用Microsoft®VisualWebDeveloper®2010Express进行调试,在IE上进行调试时,它是免费且易于使用的。

You've already marked your chosen answer so have probably found the cause by now. 你已经标记了你选择的答案,所以现在可能找到了原因。 The line in question concerns setting opacity (which IE handles as it's proprietary filter) so I suggest looking for calls to setStyle that set an opacity, perhaps ones that set an unusual value. 有问题的行涉及设置不透明度(IE处理它作为专有过滤器)所以我建议寻找设置不透明度的setStyle调用,也许是设置异常值的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM