before to post i try to search a solution in this site and by google but with now luck.
I have a issue with IE8, this code below add on the fly an IFRAME and work fine in Chrome, Firefox and IE7.
The problem is the keyHandler() function that is not triggered only in IE8.
What is the best solution to attach an event crossbrowser as onkeydown?
(also Safari and Opera will be nice to support, will be IE9 also supported with this code?)
p.s. i currently use prototype.js, the embeded blank.htm have the contenteditable on and the properly DOCTYPE -> (also in the main page where the function is called)
I post the code below and thanks in advance for suggestion and tips
function addFrame() {
var editorFrame = 'myEditor', iFrame;
var newFrame = new Element('iframe', {width: '320px', height: '70px',id: editorFrame, name: editorFrame, src:'/blank.htm'});
$('container').appendChild(newFrame);
if(document.all) {
iFrame = window.frames[editorFrame];
if (window.document.addEventListener)
iFrame.document.addEventListener('keydown', keyHandler, false);
else
iFrame.document.attachEvent('onkeydown', keyHandler); // OK IE7
}
else {
// OK Firefox, Chrome
iFrame = $(editorFrame).contentWindow;
iFrame.addEventListener('keydown', keyHandler, false);
}
}
Hmm. Several issues with your code.
document.all. It’s completely redundant these days.addEventListener, the event type does not have the “on” prefix, so you want ‘keydown’ instead of ‘onkeydown’.addEventListenerdirectly.contentWindowin all recent browsers, although it’s non-standard (contentDocument.defaultViewis the standard).keydownhandler can be applied to the iframe’s document in all browsers.I wonder if the IE 8 problem might be that
document.allmay have been removed, but I don’t really know. I haven’t useddocument.allin any code since 1999.The other possibility that occurs to me is that
window.framesuses thenameof the frame rather than its ID.UPDATE
Apologies, I didn’t test my code. Having now tested it, I realised that it’s harder than I remembered. You can’t safely attach the
keydownhandler until the iframe document has loaded, which makes things a little tricky. The easiest thing to make it work in all browsers is to handle theloadevent inblank.htmand call a function on the main page:In blank.htm, add the following:
In the main document: