Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite loading with script tag inside iframe

Here is code, stuck with that simple issue which I never have had in past 7 years:

<html>
<body>
    <iframe></iframe>
    <script>
        window.frames[0].document.write('<scr' + 'ipt type="text/javascript" src="//code.jquery.com/jquery-3.0.0.min.js"><\/scr' + 'ipt>');
    </script>
</body>
</html>

The problem is that browser's spin wheel continue to circle.

Network console shows all loaded.

If I remove iframe from DOM, or add/change @src attribute - loading stops.

like image 932
Wayne Avatar asked Aug 23 '26 13:08

Wayne


2 Answers

I believe the first answer is the better way, but I'll provide a second answer that is almost identical to your code, but shows how calling document.close() would have also solved your issue.

The issue is that you've started writing to the document's <head> element in the iFrame, but not finished (that's why the page keeps loading). Calling document.close() signals that you've finished writing to the document.

<html>
  <body>
    <iframe></iframe>
    <script>
      var doc = window.frames[0].document
      doc.write('<scr' + 'ipt type="text/javascript" src="//code.jquery.com/jquery-3.0.0.min.js"><\/scr' + 'ipt>');
      doc.close();
    </script>
  </body>
</html>
like image 195
JD Byrnes Avatar answered Aug 25 '26 04:08

JD Byrnes


Actually, I've just found solution that works if you have control on inner script (doesn't help with loading 3rd party like jQ though).

You should close "current stream" with document.close().

like image 25
Wayne Avatar answered Aug 25 '26 04:08

Wayne