Web Info and IT Lessons Blog...

Wednesday 4 March 2015

Javascript Popup Windows

Javascript is a client side scripting language used for interaction between user and web browser. When a user performs a certain action, javascript is used to show the results of the action performed in the web browser. It has nothing to do with the server side actions performed in a website or a web application. But javascript can be used to fetch data from server using ajax and a server side language like PHP.

Now let us start our javascript journey from the scratch. Javascript can be written in an html file using script tags i.e


<script type="text/javascript"></script>

Any code you write within the above script tags will be treated as javascript code. The simplest thing that we can do in javascript right now, is to write some text using javascript. Check the code below for writing text using javascript.


<script type="text/javascript">
document.write('This is Text!');
</script>

In the above code we have just passed the text ('This is Text!') in document.write function to write the text to html document.

Javascript Popup Boxes

There are 3 types of popup boxes in javascript:

1. alert box
2. confirm box
3. prompt box

Javascript Popup Boxes

javasctipt alert box
Alert box is used whenever we want to show some information or an alert or a warning to a user. The script that follows an alert box does not run until the alert box is closed by clicking the ok button. Javascript code of an alert message is given below:


<script type="text/javascript">
alert('Warning! this is an alert message.');
</script>

javasctipt confirm box
A confirm box is used to perform a certain action when a user clicks ok button and do nothing when user clicks cancel button. An example code of confirm is given below:


<script type="text/javascript">
var action = confirm("Choose your action!");
if (action == true) {
    document.write('You confirmed the action');
} else {
    document.write('You cancelled the action');
}
</script>

In the above code the output of confirm box (true or false) based on your click is stored in a variable (action). And then the if statement decides if ok button was clicked or cancel button was clicked to perform the specific operation.

javascript prompt box
Javascript prompt box is used when we want a user to enter some information before entering a web page or running a web application. An example code of javascript prompt is given below:


<script type="text/javascript">
var name = prompt("Please enter your name", "Name");
</script>

The above prompt requires a user to enter his name before running an application or browsing a web page. The value enter in the prompt is stored in the variable (name) and the default value of the prompt is "Name".

For more lessons on Javascript subscribe on InformationBitz.com

No comments:

Post a Comment