StackedCrooked
StackedCrooked

Reputation: 35485

Javascript not executed when iframe added to document?

It seems that adding an iframe to my html document prevents Javascript from being executed. In the following code sample the alert will not be shown:

<html>
    <body>
        <iframe src="http://www.google.com" />
        <script type="text/javascript">            
            alert("test");
        </script>
    </body>
</html>

What am I doing wrong?

Upvotes: 1

Views: 217

Answers (1)

bobince
bobince

Reputation: 536349

You're using the ‘self-closing’ XML syntax for <iframe> in a page that's being parsed as text/html and not XML (application/xhtml+xml).

Browsers parsing HTML don't know about this syntax; you have to use <iframe>...</iframe>. The content between the start and end tag is fallback content for browsers that don't support iframes, but there aren't really so many of those any more. Nevertheless, HTML still requires the end-tag.

Upvotes: 4

Related Questions