5

If I press the form send button "Saada" in IE11, I will get an error:

Object doesn't support property or method 'attachEvent'

I have found that this is an IE11 problem, but the only solution I have found is to use my developer tools and set the browser to run in IE9 mode for example.

But I want everyone to use my website without using their developer tools.
Do you know some other solution I could try. Or maybe I have to import some other Jquery libraries?

Other browsers work fine, but this only happens in IE11

NatNgs
  • 874
  • 14
  • 25
student
  • 73
  • 1
  • 1
  • 11

2 Answers2

13

The javascript method attachEvent was replaced with the method addEventListener in IE11.

JQuery 1.10.1 still uses this method in case of IE > 8. This will cause javascript compilation errors.

JQuery 1.10.2 seems to have solved this problem.

Hope this will help

Babar Sajjad
  • 195
  • 1
  • 4
7

attachEvent is a deprecated function used in older versions of Internet Explorer. For modern browsers use this instead.

el.addEventListener(evt,func,false);

See documentation here

You could also create a function which checks which function to use

function addListener(el, event, func){
    if (el.addEventListener) {
       el.addEventListener(event, func, false);
    }
    else {
       el.attachEvent("on"+event, func);
    }
}

Then you can attach your event by doing this:

var element = document.getElementById('myElement');
addListener(element, 'click', function(){
    alert('You have clicked!');
});

If you are unable to to this then perhaps a polyfill will work instead. Try to insert this somewhere:

if(!document.attachEvent){
  Object.prototype.attachEvent=function(event,func){
    this.addEventListener(event.split('on')[1], func);
  }
}

Hope this helps

Jackson
  • 3,476
  • 1
  • 19
  • 29
  • Okay, Can I simply add this function to the bottom of my page and it repair everything automatically? – student Feb 22 '15 at 15:23
  • Yes you can add it to the end of your js if you like. I have added an examle of how to use it – Jackson Feb 22 '15 at 15:31
  • I am using Gravity Forms plugin for Wordpress and I can't find where is the code for submit button click that I have to change. – student Feb 22 '15 at 15:58
  • Ah ok i didn't realise you were using a plugin. Try the polyfill code i inserted in my answer. – Jackson Feb 22 '15 at 16:09
  • 1
    Should I change something in this code? Right now it gives me one more error: Object doesn't support property or method 'exec' – student Feb 22 '15 at 16:14
  • http://stackoverflow.com/questions/25077612/jquery-not-working-with-ie-11 Found something like this, but I don't know what they did to fix this. – student Feb 22 '15 at 16:19
  • 1
    Problem Solved! I used latest jquery version and now it works with IE. But I hope everything else still works :) – student Feb 22 '15 at 17:12