3

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?

4

6 回答 6

1

解决方法是在回调函数或调用 eval() 方法时以编程方式将<script>块添加到 JavaScript 中的头部 DOM 元素。这是您在 IE 6 中完成这项工作的唯一方法。

于 2008-09-16T05:47:38.080 回答
1

与其将 JS 代码公开,不如将其包含在一个函数中(我们称之为“ doIt”)。您的框架窗口(假设它的名称是“formFrame”)有一个父窗口(即使它不可见),您可以在其中执行 JS 代码。在该范围内执行实际的帧重写操作:

window.parent.rewriteFormFrame(theHtml);

父窗口中rewriteFormFrame的函数如下所示:

function rewriteFormFrame(html) {
    formFrame.document.body.innerHTML = html;
    formFrame.doIt();
}
于 2008-10-01T21:27:32.307 回答
0

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.

于 2008-10-01T21:14:58.220 回答
0

另一种可能的替代方法是使用 JSON,动态添加将由浏览器自动处理的脚本引用。

干杯。

于 2008-09-16T05:48:54.473 回答
0

动态评估和/或执行脚本是不好的做法。非常糟糕的做法。非常非常非常糟糕的做法。我不能强调,这是多么糟糕的做法。

又名:听起来像糟糕的设计。你想再次解决什么问题?

于 2008-10-01T21:24:27.667 回答
0

<body onload="jsWrittenLoaded()">您可以在 body 标记 ( )中使用 onload 属性。

于 2009-10-15T23:57:30.357 回答