How to execute javascript code on IFrame load

I am dynamically creating an iframe with javascript inside the body.

iframe = m_oYDOM.get("ifAdwords");

iframe.contentWindow.document.body.innerHTML = <script type=\"text/javascript\">function Test(){alert(\"Success\");Test();}</script>";

I cannot make vigilance work. Can someone help me!

+3
source share
3 answers

I got this job

iframe = document.getElementByID("iFrameID");
var oDoc = iframe.contentWindow.document;
oDoc.open();
oDoc.write('<html><body><script type=\"text/javascript\">function Test(){alert(\"success\");}Test();<\/script><\/body><\/html>');
oDoc.close();
+2
source

.innerHTML is for text-only changes ... Call the script from another * .html file. OR just add onload to the end of the iframe, for example:

<iframe src="test.html" onload="alert('Success')";>
0
source

jQuery, javascript , zip .

div , iframe:

    <div style='height: 1px; width: 1px; border: 0;' id='iframediv'></div>

, iframe onload:

     var url = "http://google.com"
     $('#iframediv').html('<IFRAME id="downloadAlbum" onload="loadFunction()" src="'+url+'"></iframe>');

URL loadFunction.

0
source

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


All Articles