3

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 am not able to get the alert to work. Can someone help me!

2
  • 1
    see this question: stackoverflow.com/questions/251420/… Commented Jul 20, 2010 at 20:06
  • The above link is about calling a function from the parent page. But I want the function to be executed on iframe load Commented Jul 20, 2010 at 20:17

3 Answers 3

2

I got this working

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

0

Heres an example using jQuery that I use to execute javascript when a frame has loaded, I use it when redirecting the user to download a zip file, to control the browser after the download dialog appears.

Add a div to the page that will hold the iframe:

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

Then call the following to create the iframe with the onload action:

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

Change url and loadFunction as required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.