Javascript Alert

Alert boxes are used to provide the user with important information. When users enter your site and you want to make sure that they sign your guestbook, you can use an alert box to remind them. Keep in mind, that overusing this function can get very annoying and your visitors might decide not to come back.

Alert Box Example:

<html>
<head>
<title>Alert Boxes</title>
<script>
<!-- Hide Script
	alert("My Own JavaScript Alert Box!")
//End Hide Script-->
</script>
</head>
<body>
<noscript>
  This page uses JavaScript.
  Please enable it or upgrade your browser.
</noscript>
</body>
</html>

alert(“My Own JavaScript Alert Box!”), that’s the code for your alert box. Put any text inside the quotation marks in alert() and it will pop up next time you load the page.

The JavaScript alert box will always say “JavaScript Application” and there’s no way to code around it. We suppose it’s a security feature so that a site owner can’t trick visitors into something using an alert box.

You might have seen that we used the <noscript> tag above. That’s because in non-JavaScript browsers (JavaScript turned off or not supported) a message will show up saying “This page uses JavaScript. Please enable it or upgrade your browser.”.

Hello World!

“Hello World!” is generally the first program you learn to write when you start with a programming or scripting language. Are you are new to JavaScript? In this tutorial you will learn how to use the document.write function to place text on a page using JavaScript. This tutorial will also show you, how to hide your scripts from older browsers and how to place comments in your scripts.

Hello World Example:

<html><head><title>Hello World</title></head>

<body>

<script>

 document.write("Hello, World!")

</script>

</body>

</html>

Our script is in between the <script> tags; <script> tells the browser to expect our JavaScript, </script> tells the browser to expect HTML again. You can have as many scripts as you want in your source code, this also means you can therefore run multiple scripts on the same page.

document.write(“Hello, World!”) is your JavaScript. It takes the document window and writes “Hello, World!” into it.

Of course your script can not be interpreted by old browsers, such as Netscape 1.x, Internet Explorer 3.x and earlier, and the America Online browser before version 4. While well working browsers are told to ignore what they do not “understand”, we use a technique to make sure.

Script Listening 1.2:

<html><head><title>Hello World</title></head>

<body>

<script><!-- Hide Script

 document.write("Hello, World!")

//End Hide Script-->

</script>

</body>

</html>

This will look like a comment to older browser and they will simply ignore it and not produce an error. In case you want to display a message to your site?s user if his browser doesn?t support JavaScript, add your message into the <noscript> tag.

The more advanced your scripts get, the more it becomes necessary for you to write comments inside your scripts. For example we could forget what a certain statement means and in order not to need to look it up in a book or here at HTMLcenter, you can write a comment next to it which explains what it does. If you distribute your scripts on your website, you could use comments to write a short notice, that tells who it was written by and where to find it, and other scripts.

Script Listening 1.3:

<html><head><title>Hello World</title></head>

<body>

<script>/* Script name: Hello, World!

   HTMLcenter - JavaScript Tutorial

   Date: 03-07-1999

*/

<!-- Hide Script

 document.write("Hello, World!")

// document.write prints text on the screen

//End Hide Script-->

</script>

</body>

</html>

There are two different ways to write comments. You can use “/*” and “*/” if the comments are more than one single line, instead of writing “//” at the beginning of each. We use “//” if your comments fit on one single line.

This is the end of our “Hello World” tutorial. You learned how to print a message on the screen, how to hide your scripts from older browsers or browsers that have JavaScript turned off, and you learned how to write comments inside your JavaScript. We suggest that you check out our next JavaScript tutorial and keep on scripting!

Please note, that if any of the HTML tags, you have read while going over this tutorial confuse you, or you are not quite sure about their meaning, please check out our HTML tutorials (Basics) at HTMLcenter.