12/18/2010

Hộp thoại ok cancel javascript !

sau đây là các lệnh javascript hiển thị hộp thoại cảnh báo ok/cancel
 
<form name=myform>
<input type=button value="Try it now"
onClick="if(confirm('Ban co muon tat may khong'))
alert('Ban da dong y chon');
else alert('Ban da huy bo chon')">
</form>

A CONFIRM box is used when you are trying to get a YES / NO answer from the visitor. The OK button acts as the YES response the CANCEL button acts as the NO response.

To get the CONFIRM actions to perform, we have to use a VARIABLE situation to control it. That means, we have to use a bit of algebra to temporarilly assign a value to the visitor's answer choice. After the value (answer) is determined, the script then carries out the set of instructions accordingly. This is usually done with an IF...ELSE statement. IF the answer is true, do this. ELSE the answer is false, so do this instead.

Here is the code used for my example :
<script type="text/javascript">
<!--

var answer = confirm ("Are you having fun?")
if (answer)
alert ("Woo Hoo! So am I.")
else
alert ("Darn. Well, keep trying then.")

// -->
</script>

VAR means VARIABLE. A temporary storage place.

ANSWER is the name of that storage place. Depending on the visitor's response, the ANSWER will hold either a TRUE or FALSE value.

CONFIRM creates the actual pop-up box with an OK and CANCEL choice. OK is used for a TRUE response. CANCEL is used for a FALSE response. The text area following is used as the prompt text on the box. Usually a true/false type question.
<script type="text/javascript">
<!--

var answer = confirm ("Are you having fun?")
if (answer)
alert ("Woo Hoo! So am I.")
else
alert ("Darn. Well, keep trying then.")

// -->
</script>

IF( ) ELSE . This determines what happens according to the input. It looks at the storage cell called ANSWER. IF it is a TRUE value, it does the first set of instructions. ELSE it is a FALSE value, so do this other set of instructions.

The CONFIRM pop-up can be used to direct the visitor to a specific page or site depending on the choice too. Here is an example that uses the CONFIRM to ask IF the visitor would like to continue into the site, or be directed to the Yahoo site. This is placed in the HEAD area of the coding :
<script type="text/javascript">
<!--

var answer = confirm ("Please click on OK to continue loading my page, or CANCEL to be directed to the Yahoo site.")
if (!answer)
window.location="http://www.yahoo.com/"

// -->
</script>

Since the code is in the HEAD area, it is read and implemented before the loading of the actual page in the BODY area.

Notice the ! before the word answer in the IF statement. That means NOT. Normally a confirm question is looking for the OK answer. To find the CANCEL answer, we have to tell it to look for a negative answer, or NOT. It then carries out the instructions as : IF this statement is true, THEN do this set of instructions, otherwise carry on and load the page.

window.location tells the browser to load the following URL address into the current browser window location. Since it will be loading in the new URL, the page with the CONFIRM box will be left behind.

SHARE THIS

Author:

Tôi là PHƯỚC kỹ sư công nghệ thông tin. chuyên thiết kế web và làm dịch vụ MMO.

0 comments: