Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript: possible for parent to kill child iframe if it is stuck in an infinite loop?
    primarykey
    data
    text
    <p>I have a page that has an iframe with external content. I don't want infinite loops in the external content to crash my whole page. Is there any way to get around this.</p> <p>I tried to set something up where the parent postMessages the child iframe every so often and if the child Iframe doesn't respond for too long a time the parent changes the iframes src, but this doesn't seem to work. The parent's setTimeout functions no longer execute once the iframe starts looping. See my code here (note that it will crash your tab if you execute it, open the console before execution to view the logging):</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; var scr = 'script'; var html = '&lt;html&gt;&lt;head&gt;&lt;script&gt;\n' + ' window.addEventListener("message", answer, false);' + ' function answer() { console.log("answered"); parent.postMessage(\'hi\', \'*\');}' + ' setTimeout("while(1){console.log(\'in loop\')};", 3000)' + "&lt;/" + scr + "&gt;&lt;/head&gt;&lt;body&gt;IFRAME&lt;/body&lt;/html&gt;"; var lastAnswer = (new Date()).getTime(); var iframe = document.createElement('iframe'); queryChild(); window.addEventListener("message", receive, false); function receive() { lastAnswer = (new Date()).getTime(); console.log('got answer'); } function queryChild() { console.log('querying'); if((new Date()).getTime() - lastAnswer &gt; 5000) { console.log('killing'); iframe.src = ''; } else if(iframe.contentWindow){ iframe.contentWindow.postMessage('hi', '*'); } setTimeout(queryChild, 2000); }; document.body.appendChild(iframe); iframe.contentWindow.document.open(); iframe.contentWindow.document.write(html); iframe.contentWindow.document.close(); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Any suggestions on how to solve this problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload