简体   繁体   中英

Javascript in an Iframe not executing

I'm a bit of a noob at javascript so help me out. I have this at the bottom of my body tag:

<iframe id="scriptFrame" name="scriptFrame" height="0" width="0">
<script type="text/javascript" language="javascript">
alert("Hello World");
parent.alert("Hello world");
function foo()
{
   alert("Hello World");
}
</script>
</iframe>

Neither alert pops up. What am I doing wrong? I tried adding html and head tags but that didn't help. I also tried doing document.getElementById('scriptFrame').contentWindow.foo() and frames['scriptFrame'].foo() but neither worked

Note that You cannot add any content directly into an <iframe> tag. Even using JavaScript, you can add content only if the page in the iframe is from the same domain as the parent page. So you will have to do following:

<html>
<body>
<script >
function foo()
{
   alert("Hello World");
}
</script>
<iframe id="scriptFrame" onload="foo()"name="scriptFrame" height="0" width="0"> 
</iframe>
</body>
</html>

Calling a parent JS function from iframe is possible , but only when both the parent and the page loaded in the iframe are from same domain ie abc.com, and both are using same protocol ie both are either on http:// or https://.

The call will fail in below mentioned cases:

  1. Parent page and the iframe page are from different domain.

  2. They are using different protocols, one is on http:// and other is on https://.

Any workaround to this restriction would be extremely insecure.

For instance, imagine I registered the domain superwinningcontest.com and sent out links to people's emails. When they loaded up the main page, I could hide a few iframes in there and read their Facebook feed, check recent Amazon or PayPal transactions, or--if they used a service that did not implement sufficient security--transfer money out of their accounts. That's why JavaScript is limited to same-domain and same-protocol.

The content of an iframe is never rendered. When specifying something inside of the iframe, that is the fallback for content loaded with the src attribute (for browsers that do not support iframe).

So either use onload for the iframe or put the content of the iframe in a separate page and specify it in the src-attribute.

You must use the src attribute of the iframe with the URI where the iframe will load its content.

The information to be inserted inline is designated by the src attribute of this element. The contents of the IFRAME element, on the other hand, should only be displayed by user agents that do not support frames or are configured not to display frames.

You have the script inside the iframe tag, so it will only be shown on browsers without frames support.

You must have <html> in the iframe and the <script> tag must be either in the <head> or in the <body> .

Also, note the the language attribute of <script> is deprecated.

If you are trying to run javascript when iframe is loaded, try this:

<html>
    <head>
        <script type="text/javascript">
            function someFunction(){
                //some stuff here
            }
        </script>
    </head>
    <body>
        <iframe id="scriptFrame" name="scriptFrame" height="0" width="0" onload="someFunction();"></iframe>
     </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM