I’m trying to build a web site that cleanly separates the HTML presentation from the JavaScript behavior.
To gain experience in this, I have created a simple “Hello, world!” site with one page and one behavior. The page contains a “Hello, world!” message, and the JavaScript displays a similar message to the user through an alert box.
To implement this, I have created two files on my local machine called index.html and sayhello.js.
The HTML file index.html contains this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Hello, world!</title>
</head>
<body>
<h1>Helo, world!</h1>
<p>That's all for now.</p>
<script type="text/javascript" src="sayhello.js"/>
</body>
</html>
The JavaScript file sayhello.js contains this:
alert('Hello, world!');
My operating system is Windows 7.
I open index.html in Opera 11 and see a “Hello, world!” dialog pop up as soon as the page opens.
In Firefox 4, Chrome 11, and IE 9, I don’t see the pop up. It’s as if the JavaScript is being ignored.
Why doesn’t the alert box show up in all browsers? What can I do to make it show in all?
The
<script>tag is not self-closing. You need to write