Document.open/document.write does not correctly clear a document in chrome - is this an error in chrome?

I write in an iframe via document.write, then try to overwrite the document on the same iframe. In FF, this works correctly. However, in the chrome code from the original document .write is saved even after I overwrite it with the second document.write.

See this script: http://jsfiddle.net/meQcC/

If you view it in FF, as you would expect, the iframe is empty and you actually get the โ€œonLoad function not defined errorโ€, because in the line

doc.write("<html><head><script>;" + "<\/script></head><body onload='onLoad()'></body></html>"); 

Obviously, the onLoad function is not defined. However, if you look at the same script in chrome, the iframe will display a black rectangle, and there will be no error with respect to calling onLoad, it will call the previously defined function, as if it still exists !!!!

Is there a way to clear a document in chrome so that I can overwrite the contents of the iframe without any old code? Is this a mistake in chrome?

+4
source share
2 answers

Yes, this is a bug in Chrome (or rather, in WebKit). Per spec, it must create a new Window object and delete all global event listeners, and it does not.

In particular, see http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#dom-document-open step 14.

+5
source

Removing <script> does not define the undefined functions that it defines.

If you want to achieve this, you need to save the list of all global combinations that you create and delete them with delete window.WHATEVER;

+2
source

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


All Articles