How can I create JavaScript created using document.write () to execute?

I have a multi-frame layout. One of the frames contains a form that I submit via XMLHttpRequest. Now when I use document.write () to rewrite the frame with the form, and the new page I add contains any javascript, then javascript will not be explicated in IE6?

For instance:

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

In the above case, the content of the page is replaced by a test, but alert () is not executed. This works fine in Firefox.

What is the workaround for this problem?

+3
source share
6 answers

<script> DOM JavaScript eval(). IE 6.

+1

, JS- , ( "doIt" ). ( "formFrame" ) ( ), JS-. :

window.parent.rewriteFormFrame(theHtml);

rewriteFormFrame :

function rewriteFormFrame(html) {
    formFrame.document.body.innerHTML = html;
    formFrame.doIt();
}
+1

JSON, , .

.

0

In short: you cannot do this. However, JavaScript libraries such as jQuery provide functionality to do just that. If you are dependent on this, give jQuery a try.

0
source

Eval and / or running scripts dynamically is bad practice. Very bad practice. Very, very, very bad practice. I cannot stress how bad the practice is.

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

0
source

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

0
source

Source: https://habr.com/ru/post/1696929/


All Articles