开发者

Object doesn't support this property or method javascript error in IE6 only

I've searched the internet and this great stackoverflow.com site particularly, but cannot help myself. I am not very experienced programmer and a friend of mine asked me to fix the bug that is on IE6 only. It works on FF, IE7/8, Opera. Here is the PART of the code that gives error message on IE6:

开发者_如何学编程(function(d, n, r) {
  var ie, jscript, settings = {};
  function add(element, type, listener) { element.addEventListener(type, listener, false); };
  function remove(element, type, listener) { element.removeEventListener(type, listener, false); };
  var l = {change:[], ready:[], load:[]};
  a11y = function(f) {
    a11y.change(f);
  };
})
(self.document, self.navigator);

The error is on line 7: a11y.change(f); All other browsers are OK, just IE6. I am not author of the script, just trying to fix this. Can anybody help, please? There must be some problem with function declaration or...?


The example looks pretty confused there is a lot of code there that looks to be a distraction.

From what I can see the problem is in this code:

a11y = function(f) {
    a11y.change(f);
};

Here you are defining a new function called a11y and inside the definition of that function you are calling a method 'change' on the function a11y being defined. I'm surprised that works in any browser.


For me it throws an error in IE7 and IE8 as well.

It reports the error line number as line 4 (line 3 in IE6 but IE6 is always off by one). That indicates that the error is in:

function add(element, type, listener) { element.addEventListener(type, listener, false); };  

which isn't surprising since IE doesn't implement addEventListener. The equivalent call on IE is:

element.attachEvent("on" + type, listener)  

It looks like the page is using jQuery, so you should probably use jQuery methods for this.
Instead of calling

add(element, type, listener)  

you could call

$(element).bind(type, listener)

You would also need to fix the "remove" function in line 5.


thanks for all your help. As suggested by Sean Hogan, the problem was that IE doesn't implement addEventListener. I found similar fix somewhere on the internet and it worked for me.

  function add(element, type, listener) {
      remove(element, type, listener);
      element.attachEvent("on"+ type, listener);
  };
  function remove(element, type, listener) {
      element.detachEvent("on"+ type, listener);
  };
  settings.jscript = jscript = @_jscript_version;
  settings.ie = ie = parseFloat(n.userAgent.split("MSIE ")[1]);
   if (d && d.execCommand) {
    try { d.execCommand("BackgroundImageCache",false, true); }
    catch (e) { }
  }

Thanks again!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜