Raapwerk
Raapwerk

Reputation: 629

Can't access iframe content in IE

I'm building a CMS application which I'm debugging now for IE. But things happen I cant comprehend.

First I have a div with the content of a page. I extract the content of the div and with javascript I create an iFrame and append that to the div. This is for making the WYSIWYG-editor. Then when I want to add the content to the iFrame the problems start. IE can't access the iFrame's body for some reason. I can alert the iFrame's document, which gives me [object Document] or [object HTMLDocument], but when I then alert the body I get null.

I use this function for getting the document as part of a javascript object:

iframedoc: function(aID) {
    if (document.getElementById(aID).contentDocument){
        return document.getElementById(aID).contentDocument;
    } else {
        var iFrame = document.getElementById(aID);
        return iFrame.contentWindow.document;
    }
},

and this is where I call it for the first time:

tas[i].innerHTML = "";
tas[i].appendChild(ta_edit_functions);
tas[i].appendChild(ta_edit);
tas[i].appendChild(ta_result);
alert(ta.iframedoc(ta_edit_id));
ta.iframedoc(ta_edit_id).body.innerHTML = ta_edit_content;
ta.iframedoc(ta_edit_id).designMode = 'on';

Here I empty the div, append a div containing the WYSIWYG-functions, the iFrame and the textarea for eventually submitting the form.

I'm testing in IE 8 and 9. Weirdest thing is that when I put an alert before first calling iframedoc in IE 8 I suddenly do get the body.

I would be really grateful if someone could at least point me in the right direction. I really don't have a clue what's happening.

Upvotes: 1

Views: 1569

Answers (2)

Raapwerk
Raapwerk

Reputation: 629

Eventually my solution was using a interval function checking if the body of the iFrame is available.

But with those errors gone I found out that in IE8 turning on designmode for the iFrame removed all the content of it. So I ended up just using a contenteditable div..

Upvotes: 0

user149341
user149341

Reputation:

I'm testing in IE 8 and 9. Weirdest thing is that when I put an alert before first calling iframedoc in IE 8 I suddenly do get the body.

That sounds as though you're going too fast for the browser -- the iframe is probably just not fully loaded yet when you try messing with it, so adding in an alert gives it the extra time it needs. Try deferring some of your code using an onload event on the iframe element.

Upvotes: 4

Related Questions