Test your (or your browser's) JavaScript knowledge with a simple HTML page 

Joined:
04/09/2007
Posts:
773

February 25, 2011 08:43:40    Last update: February 28, 2011 11:16:10
If you think the JavaScript code below is simple, think again. Try it in different browsers and see if you can explain what you see.
  1. What is displayed in the browser window with this HTML page?
    <!doctype html>
    <html>
    	<body>
    		<p>before script</p>
    		<script type="text/javascript">
    			document.writeln("<h1>Hi!</h1>");
    			document.close();
    		</script>
    		<p>after script</p>
    	</body>
    </html>
    

  2. What is displayed inside the iframe? Try different browsers.
    <!doctype html>
    <html>
    	<head>
    		<style type="text/css">
    		iframe {
    			width: 450px;
    			height: 275px;
    		}
    		</style>
    	</head>
    	<body>
    		<iframe id="theIframe"></iframe>
    		<script type="text/javascript">
    		window.onload = function() {
    			var doc = document
    					  .getElementById('theIframe')
    					  .contentWindow
    					  .document;
    			doc.writeln('<!doctype html>\n'
    			          + '<html><body>\n'
    			          + '<p>before script</p>\n'
    			          + '<script type="text/javascript">\n'
    			          + 'document.writeln("<h1>Hi!</h1>");\n'
    		              + 'document.close();\n'
    			          + '</' + 'script>\n'
    			          + '<p>after script</p>\n'
    			          + '</body></html>');
    			doc.close();
    		}
    		</script>
    	</body>
    </html>
    

Share |
| Comment  | Tags