Sthe
Sthe

Reputation: 2705

frame.addEventListener doesn't work on IE

I have a document that loads into an iframe. Now I would like to know whenever the "body" element of that document gets the event "focus". I've had success using addEventListener in other browsers, but IE (8 to be specific) gives me this error:

Object doesn't support this property or method

I implemented it like this:

//This works in Chrome, Opera, Safari and Firefox but not in IE

var myFrame = document.getElementById("my-i-frame");
var frame = (myFrame.contentWindow || myFrame.contentDocument);
frame.addEventListener("focus", function(){alert('works')},false);

I also tried:

frame.document.addEventListener("focus", function(){alert('works')},false);

Before you ask WHY WOULD YOU DO THAT?, let me explain:

I've created an html WYSIWYG editor and I would like resize my iframe when the user clicks inside the frame to start editing. To do that, the only method I could think of is having an "focus" event and then do what I want to do whenever it's fired. I hope that sounds clear (otherwise forgive me. I'm not very English speaking :-) )

Upvotes: 0

Views: 3242

Answers (2)

epascarello
epascarello

Reputation: 207557

IE8 does not support addEventListener.

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

Try:


if(iframe.addEventListener)
   iframe.addEventListener('load', func, true);
else if(iframe.attachEvent)
   iframe.attachEvent('onload',func);

Upvotes: 2

Related Questions