views:

1058

answers:

8

OK, every other browser works fine with the method I have coded so far but for some reason Internet Explorer will not work. I have spent hours of time (more time than actually developing the feature!) on compatibility and am close to giving up!

I have a forum and one of its neat features is the WYSIWYG editor. For that, I essentially have an IFrame that acts as the document:

<iframe name="writer" src="/scripts/blank.html" class="writer"></iframe>

This is the current state of the JavaScript (constantly updated):

function initEditor()
{
    w = frames['writer']
    wc = g('writerCopy')

    if(w == null) return

    frames['writer'].document.designMode = 'on'
    frames['writer'].document.body.innerHTML = styleSheet+wc.value
    frames['writer'].focus()
}

It works partially now, but fails on the line:

frames['writer'].document.body.innerHTML = styleSheet+wc.value

in Internet Explorer with "'frames.writer.document.body' is null or not an object".

+2  A: 

Have you tried putting semicolons at the end of your javascript lines?

RedFilter
And use === to compare null
StingyJack
Should I be? I'll try it now.
That doesn't matter in his case.
jishi
It's generally easier to read for other JS coders if semicolons are used. I don't think this affects the results, however.
strager
+2  A: 

Have you activated IE's debugging facilities?

Adriano Varoli Piazza
+1  A: 

I'm not even sure IE supports that designMode.

And, .contentDocument is only IE8, IE7 and less uses .contentWindow.document, but iframe windows are part of the frames-collection.

try this, should be crossbrowser:

<iframe name="writer"></iframe>

frames["writer"].document.body.innerHTML = "some html...";
jishi
I was just getting ready to suggest the frames collection
Isaac Dealey
Changed, it works far better now - thanks! But it still fails (as noted above).
A: 

Am I missing something here? shouldn't you use something like:

window.frames[nameOrNumberOfFrame]...

See also in MSDN:

This collection contains only window objects and does not provide access to the corresponding frame and iframe objects. To access these objects, use the all collection for the document containing the objects.

Dror
I am using window.frames now. Are you suggesting I should use the <i>all</i> collection instead?
I am suggesting you use what ever Microsoft declares you should use - its their browser after all...
Dror
+1  A: 

You need to point your iframe to a dummy document for IE. Just create a file blank.html with the following:

<html><body></body></html>

and set <iframe src="blank.html" ... >

Then you can go about referencing frame.document.body.innerHTML = '...' to your hearts content.

BTW that is a terrible title to a question.

Crescent Fresh
I think this is his problem... and I've update the title.
Prestaul
Thanks for the idea but it still gives the same error. I checked manually that the /blank.html page loads as it should and included the exact code above.
It'd be easier to just use <iframe src="about:blank" ...> -- there's no need to create another file when the browser is perfectly capable of generating a blank page.
Evan Hanson
A: 

In the end I used frames['frameName'].document.write('someText') but only if the other method fails.

A: 

Evidently IE8 does not make frame elements available until the entire parent page has loaded. Also note, you can write to the frame before the parent page loads, but this will overwrite the frame and prevent it from being loaded.

The easy solution is to move the InitEditor() call from inside the body to here:

<body onload="InitEditor()">
A: 

Perhaps the iframe isn't loaded yet. I can duplicate your "'frames.writer.document.body' is null or not an object" error. I added a setTimeout around it and it then worked for me.

setTimeout(function () {
    frames['writer'].document.body.innerHTML = "some text";
}, 200);
Adam