firefox iframe getComputedStyle错误的Javascript解决方法

时间:2015-09-18 19:38:13

标签: javascript firefox iframe

我正在尝试对隐藏的iframe / getComputedSytle firefox错误548397使用以下解决方法。

if (/firefox/i.test(navigator.userAgent)){
   window.oldGetComputedStyle = window.getComputedStyle;
   window.getComputedStyle = function (element, pseudoElt) {
      var t = window.oldGetComputedStyle(element, pseudoElt);
      if (t === null) {
         return {};
      } else{
         return t;
      }
   };
}

但在我的情况下,我还需要getComputedSytle.getPropertyValue,即我收到以下错误:

TypeError: my_window.getComputedStyle(...).getPropertyValue is not a function

如何将getPropertyValue添加到上述解决方法中?

2 个答案:

答案 0 :(得分:4)

您可以创建一个空函数:

if (/firefox/i.test(navigator.userAgent)){
   window.oldGetComputedStyle = window.getComputedStyle;
   window.getComputedStyle = function (element, pseudoElt) {
      var t = window.oldGetComputedStyle(element, pseudoElt);
      if (t === null) {
         return {
            getPropertyValue: function(){}
         };
      } else{
         return t;
      }
   };
}

答案 1 :(得分:1)

我认为更好的解决方案就是这个

function setFirefoxPolyfill() {
  if (/firefox/i.test(navigator.userAgent)){
    window.oldGetComputedStyle = window .getComputedStyle;
    window.getComputedStyle = function (element, pseudoElt) {
      var t = window.oldGetComputedStyle(element, pseudoElt);
      if (t === null) {
        return element.style;
      } else{
        return t;
      }
    };
  }
}

如果是null响应,您只需返回包含所有原型方法和字段的元素样式

相关问题