views:

426

answers:

2

For example:

script.js:

function functionFromScriptJS() {
    alert('inside functionFromScriptJS');
}

iframe.html:

<html>
<head>
<script language="Javascript" src="script.js"></script>
</head>
<body>

<iframe>
  <body>
     <script language="JavaScript">
       functionFromScriptJS();
     </script> 
  </body> 
</iframe>

</body>
<html>

The above call to functionFromScriptJS() is not working.

The first guess parent.functionFromScriptJS() is not working too.

Is it possible to access such an external function from an iframe when the include is not in the iframe itself but in the parent document?

@Edit: So my mistake was that I put the document inside the iframe tag, and I sould have put it in a separate file and specified through the src attrubute of the tag. In this case parent.functionFromScriptJS() works.

+2  A: 

Iframes do not support inline content. you must use the src attribute to reference a different file. The inner text of the <iframe> tag will be displayed to browsers that do not support iframes.

Example:

<iframe src="someFile.html" width="100%" height="300px">
  <p>Your browser does not support iframes.</p>
</iframe>

Once you have your page loading then you should be able to call the script using parent.functionFromScriptJS();

Ryan Cook
Thanks for the note! I'll rewrite the example and try it out.
axk
A: 

The best solution here I think would be not to use an iframe (although not formally deprecated, it does not appear in the XHTML 1.1 spec) and to use an AJAX get to grab the page and load it into a

Then javascript functions will work as normal!

adam