Script visibility includes in IFRAME

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 function call from FromScriptJS () does not work.

The first guess parent.functionFromScriptJS () doesn't work either.

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 an iframe tag and I could put it in a separate file and specify it via the src attribute of the tag. In this case, parent.functionFromScriptJS () works.

+1


source to share


2 answers


Iframes do not support inline content. you must use the src attribute to link to another file. The inner text of the tag <iframe>

will be displayed in browsers that do not support iframe.

Example:



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

      

After you load the page, you should be able to call the script with parent.functionFromScriptJS();

+2


source


The best solution here, I think, would be not to use an iframe (although not formally recommended, it doesn't show up in the XHTML 1.1 spec) and use AJAX to grab the page and load it into <div>



Then the javascript functions will work as usual!

0


source







All Articles