Good day to all. At one of the sites I noticed that the page does not stop loading (the indicator in the tab header does not stop rotating). Found out that the reason - records in iframe through document.write (). The code (simplified) is something like this (the hang can be checked locally):

<!doctype html> <html> <meta charset = 'UTF-8'> <body> <iframe id="k_c0"></iframe> <script language="JavaScript"> var d; d=document.getElementById('k_c0').contentDocument; d.write('<div> 234</div>'); </script> </body> </html> 

What processes cause a spinning indicator? And why do they arise? Thank you in advance.

    1 answer 1

    As stated in learn.javascript.ru

    The document.write (str) method works only while the HTML page is in the process of loading. It appends text to the current HTML location even before the browser builds a DOM from it.

    document.getElementById ('k_c0'). contentDocument - returns the document. d.write - writes the frame to the document and cannot write it. Accordingly, can not load the page completely. So she turns.

    • However, the same resource says: Technically, you can call document.write at any time, however, when the HTML is loaded and the browser has completely built the DOM, the document becomes “closed”. An attempt to add something to a closed document opens it again. All current content is deleted. Therefore, why is the frame content not overwritten? - Vadim Slepakurov
    • because from the point of view of security the frame cannot be addressed from another url - Kostiantyn Okhotnyk
    • one
      Exactly not so, in your case you can write it, but it will not process the rest of the document. You have to write d.body.innerHTML ="<div>111</div>" - Kostiantyn Okhotnyk
    • If interested, link to the site: link - Vadim Slepakurov
    • but it booted up normally by the way - Kostiantyn Okhotnyk