Great Templates

FEATURED WEB TEMPLATES

Amazon Books

Learn PHP

PHP Training
Zend Cert Training Zend Certified Engineer Zend PHP Pro

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:

  1. <SCRIPT language="JavaScript">
    This tag lets the browser know you are using JavaScript commands here.

  2. <!-- hide script from old browsers
    This makes sure older browsers don't display your script as text on the page.

  3. 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.

  4. //-->
    Stops hiding the script from old browsers.

  5. </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");  


Learn PHP | Zend Certified Engineer | Zend PHP Pro | PHP Web Apps | Web Hosting Service | Low Cost Domain Names | Great Templates | Great Books | Testimonials | Tech.Articles | TOS | AUS | Home | Linux Apache MySQL PHP