Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1033785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:17:04+00:00 2026-05-16T14:17:04+00:00

before to post i try to search a solution in this site and by

  • 0

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);
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:17:05+00:00Added an answer on May 16, 2026 at 2:17 pm

    Hmm. Several issues with your code.

    1. Don’t use document.all. It’s completely redundant these days.
    2. When using addEventListener, the event type does not have the “on” prefix, so you want ‘keydown’ instead of ‘onkeydown’.
    3. Test the objects you’re about to use rather make inferences from the existence of unrelated objects. Test addEventListener directly.
    4. The branching for Chrome and Firefox is unnecessary. You can use contentWindow in all recent browsers, although it’s non-standard (contentDocument.defaultView is the standard).
    5. The keydown handler can be applied to the iframe’s document in all browsers.

    I wonder if the IE 8 problem might be that document.all may have been removed, but I don’t really know. I haven’t used document.all in any code since 1999.

    The other possibility that occurs to me is that window.frames uses the name of 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 keydown handler 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 the load event in blank.htm and call a function on the main page:

    In blank.htm, add the following:

    <script type="text/javascript">
        window.onload = function() {
            parent.iframeLoaded();
        };
    </script>
    

    In the main document:

    function addFrame() {
        var editorFrame = 'myEditor', iFrame;
    
        var newFrame = new Element('iframe', {
            width: '520', // width and height properties do not have units
            height: '200',
            id: editorFrame,
            name: editorFrame,
            src: 'blank.htm'
        });
    
        $('container').appendChild(newFrame);
    
        window.iframeLoaded = function() {
            var iframeDoc, UNDEF = "undefined";
            if (typeof newFrame.contentDocument != UNDEF) {
                iframeDoc = newFrame.contentDocument;
            } else if (typeof newFrame.contentWindow != UNDEF) {
                iframeDoc = newFrame.contentWindow.document;
            } else {
                throw new Error("Unable to access iframe document");
            }
    
            if (typeof iframeDoc.addEventListener != UNDEF) {
                iframeDoc.addEventListener('keydown', keyHandler, false);
            } else if (typeof iframeDoc.attachEvent != UNDEF) {
                iframeDoc.attachEvent('onkeydown', keyHandler);  // OK IE7
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did do some googling and searching on this site but did not find
Please try to search StackOverflow before asking a question. Many questions are already answered.
Sorry if this is a bit long winded but I thought better to post
I'm trying to create a simple search page, but I'm not 100% sure how
This should be simple, but the answer is eluding me. If I've got a
Original title: Can't fixed misconfigured routes I want to make a search based in
include/TestBullet.h:12: error: expected constructor, destructor, or type conver sion before '(' token I hate
I'm writing an app where a user can both create their own pages for
I'm using SWFUpload and Paperclip on Rails 2.3.5 to upload images and videos. How
I posted a question awhile ago asking how I could limit the rate at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.