WDN Logo

home-icon Home / JavaScript / JavaScript Online Reference – alert() method

JavaScript Online Reference – alert() method

JavaScript Online Reference - alert() method

alert() is a method of the window object. It is employed to display pop-up boxes with some text and a button labeled “OK”.

Usage of the alert() method is similar to the write() method of the window object. The text placed inside the parenthesis is displayed on the pop-up box.

window.alert("I am an alert box");

Since it’s not necessary to specify the window object we can leave it out from the code. Thus, the following code will function equally well as the one above.

alert("I am an alert box");

Sponsored Links

Why is alert() a method of the window object?

When I first came across alert(), I wondered why it is method of the window object and not of the document. The answer lies in the fact that the document object defines only the space in the browser window where an HTML document is displayed. The window object determines the entire browser area including the title bar, status bar, the buttons etc. The alert box pops up because of the browser and not the HTML document. Write() on the other hand specifies what has to be written inside the HTML space and hence is a method of the document object.

Difference between document.write() and window.document.write()

There is no difference between the two. Remember, the window object is the highest level object. It can contain other objects and their methods. Hence, document is a object contained inside the window object; write() is a method of the document object and is thus, specified after the document object.
Explicit declaration of the window object is not necessary thus, document.write() works just like window.document.write()

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 … at least I have never used them!)

alert("JavaScript\nis\na\nclient-side\nprogramming\nlanguage");

This looks really messed up. Click here to see the result.
You’ll notice that new lines have been introduced at each occurrence of the \n escape character.

Sponsored Links

Search

Your comments
Star icon IMPORTANT Have a question / problem? Click here to ask an expert.

JOKE - Being a Programmer

Sponsored Links

Tips

How do I view a deleted web page?
Is there a way to view a deleted web page - one that is no longer available? Yes there is and the solution is quite simple. [more...]

Fact

About 8 billion electrons are used for an average email of 50KB. If one were to weight all these, it turns out that the message weighs about "two ten thousandths of a quadrillionth of an ounce". [more...]

Share on Whats app Share on Facebook Share on Twitter Share on Google plus