views:

595

answers:

6

I have a multi-frame layout. One of the frames contains a form, which I am submitting through XMLHttpRequest. Now when I use document.write() to rewrite the frame with the form, and the new page I am adding contains any javascript then the javascript is not exectuted in IE6?

For example:

document.write("<html><head><script>alert(1);</script></head><body>test</body></html>");

In the above case the page content is replaced with test but the alert() isn't executed. This works fine in Firefox.

What is a workaround to the above problem?

+1  A: 

Workarount is to programmatically add blocks to head DOM element in JavaScript at Callback function or call eval() method. It's only way you can make this work in IE 6.

dimarzionist
A: 

Another possible alternative is to use JSON, dynamically adding scripts references which will be automatically processed by browser.

Cheers.

dimarzionist
Could you explain this in more detail? I don't think this will work, but maybe I just don't understand what you are suggesting.
SpoonMeiser
A: 

In short: You can't really do that. However JavaScript libraries such as jQuery provide functionality to do exactly that. If you depend on that, give jQuery a try.

Armin Ronacher
A: 

Eval and/or executing scripts dynamically is bad practice. Very bad practice. Very, very, very bad practice. I can't stress enough, how bad practice it is.

AKA.: Sounds like bad design. What problem are you trying to solve again?

roosteronacid
+1  A: 

Instead of having the JS code out in the open, enclose it in a function (let's call it "doIt"). Your frame window (let's say it's name is "formFrame") has a parent window (even if it's not visible) in which you can execute JS code. Do the actual frame rewrite operation in that scope:

window.parent.rewriteFormFrame(theHtml);

Where rewriteFormFrame function in the parent window looks something like this:

function rewriteFormFrame(html) {
    formFrame.document.body.innerHTML = html;
    formFrame.doIt();
}
Ates Goral
A: 

You could use an onload attribute in the body tag (<body onload="jsWrittenLoaded()">).

Ollie Saunders