7

Does anyone know why this code doesn't work. This means, the alert is NOT fired

<iframe/>
<script type="text/javascript">alert('hello');</script>

While this code with the alert BEFORE the Iframe works perfeclty. This means the alert is fired

<script type="text/javascript">alert('hello');</script>
<iframe/>

Seems that no javascript placed after the iframe is executed, I don't find any logic to this.

1 Answer 1

11

You need to close the <iframe> tag like this:

<iframe></iframe>
<script type="text/javascript">alert('hello');</script>
Sign up to request clarification or add additional context in comments.

4 Comments

I should add that the reason this is the answer is that <iframe/> is not really a valid tag. They can't be closed in that fashion.
They can be closed in that fashion, but only in XHTML, and not in HTML-compatible XHTML (i.e. HTML served as text/html so that Internet Explorer will parse it because it doesn't support XHTML)
I have to wait some minutes in order to be allowed to accept an answer, Stack Overflow rules. Not sure why they did it like this.
+1 good thing I only spent 10 minutes before searching stack overflow! :)

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.