views:

78

answers:

3

This happens in all browsers, so there must be a reason.

Example:

<html>
<body>
<script>var a="support/";
var aa='<iframe src="http://google.com/' + a + '" />'; 
document.write(aa)</script> 
<script>alert('test')</script>
</body>
</html>

The code after the iframe write (in this case alert('test')) doesn't execute. Why?

+9  A: 

Because you're writing broken HTML into the document -- there's no > on the iframe tag.

Adam DiCarlo
Whoops, but even with the properly formatted HTMl, it still doesn't work. Edit: never mind, it does work.
Michael Vasquez
+4  A: 

You need to close your quotes and iframe. This works:

<html>
<body>
<script>var a="support/";
var aa='<iframe src="http://google.com/' + a + '" />';
document.write(aa)</script> 
<script>alert('test')</script>
</body>
</html>
Andyourname
Why have people upvoted this? It's wrong, try it yourself!
Michael Vasquez
+5  A: 

Your HTML that you write into the document is invalid and causes the browser to fail interpreting the rest of the document, including the remaining <script> tags.

You are writing <iframe src="http://google.com/support/. Add "></iframe> and it's ok.

However, a cleaner approach would be not to use document.write at all (assuming you have some HTML element with id="container" to hold the iframe):

var iframe = document.createElement('iframe');
iframe.setAttribute("src", "http://google.com/support/");
document.getElementById("container").appendChild(iframe);
Techpriester