Sunday 25 January 2015

How to show an Alert Message Box in Javascript

In this tutorial, we will show you how to show an Alert Message Box in Javascript. This is a very simple script and explained in detail with 3 set of examples.
Alert Message Box

Method used for Alert Message Box:

alert() - The alert() method shows an alert box with a specified message and an OK button.
Note: Do not overuse this method, as it prevents the user from accessing other parts of the page until the box is closed.

Example-1:

<HTML>
<HEAD>
<SCRIPT LANGUAGE=”JavaScript”>
function MsgBox (textboxstring) {
alert (textboxstring) }
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<INPUT NAME=”textbox1″ TYPE=Text>
<INPUT NAME=”submit” TYPE=Button VALUE=”Show Me” onClick=”MsgBox(form.textbox1.value)”>
</FORM>
</BODY>
</HTML>

Example-2:

This is an another method for single line Alert Message Box.
<!DOCTYPE html>
<HTML>
<BODY>
<p>Just click the button to show an alert box.</p>
<button onclick=”getMessageAlert()”>Show Me</button>
<script>
function getMessageAlert() {
alert(“This is a Single Line Alert Box!”);
}
</script>
</BODY>
</HTML>

Example-3:

Click the button to demonstrate line-breaks in a popup box.
Alert box with line-breaks:
<!DOCTYPE html>
<HTML>
<BODY>
<p>Just click the button to show line breaks in a popup box.</p>
<button onclick=”getMessageAlert()”>Show Me</button>
<script>
function getMessageAlert() {
alert(“This is First Line.\nThis is Second Line”);
}
</script>
</BODY>
</HTML>

How to use the Example:

1). Go to START > Type NOTEPAD in Run
2). Copy and paste any example in Notepad > Save NOTEPAD file as .html
3). Now run the file in Web Browser
If you have any issue with the above examples, write below in comment section.

0 comments:

Post a Comment