JavaScript: Alert Box
<HEAD>
<TITLE>Cool JavaScripts</TITLE>
<SCRIPT language="JavaScript">
<!-- hide from old browsers
alert('Welcome to my Web Site!');
//-->
</SCRIPT>
</HEAD>
This will display the alert before the page starts loading. When you hit "OK" the page will go on and load normally.
Here's the breakdown:
- <SCRIPT language="JavaScript">
This tag lets the browser know you are using JavaScript commands here.
- <!-- hide script from old browsers
This makes sure older browsers don't display your script as text on the page.
- alert('Welcome to my Web Site!');
This is your alert. Put your mesage inside the single quotes. Notice the semicolon at the end, this is what separates JavaScript commands.
- //-->
Stops hiding the script from old browsers.
- </SCRIPT>
Ends the JavaScript commands.
How to write text on multiple lines in an alert box?
We can't use the <BR> tag here, as we did in write(), because alert() is a method of the window object that cannot interpret HTML tags. Instead we use the new line escape character.
An escape character consists of a backslash (\) symbol and an alphabet. When preceeded by the backslash, these alphabet assume a special function. Here are some commonly used escapes characters:
- \n: Inserts a new line and causes the text following it to be placed on that line.
- \t: Inserts a tab
- \r: Carriage return
- \b: Backspace
- \f: Form feed
- \': Single quote
- \": Double quote
- \\: Backslash
(Note: there are other escape sequences that consist of the backslash and hexadecimal digits. Their use is rare ... atleast I have never used them!)
alert("JavaScript\nis\na\nclient-side\nprogramming\nlanguage");
|