JavaScripts :: Miscellaneous :: Alerts, Confirms and Prompts
Using Microsoft's propriety VBScript, you have a little more control of prompts and alerts. Below are some examples showing you how to add your own title to the popups. Note that VBScript is only supported by Internet Explorer. Netscape will show a regular alert, confirm or prompt. IE5 means IE5+ so IE6 will also be taken into account.
Note also that when using JavaScript and VBScript on the same page you must define a value in the language attribute. Eg.
<script language="VBScript">
or
<script language="JavaScript">
This rule is waived if the VBScript is last script tag on your page.
The Script
<script>
<!--
// Browser sniffer. Written by PerlScriptsJavaScripts.com
v3 = 0; op = 0; ie4 = 0; ie5 = 0; nn4 = 0; nn6 = 0; isMac = 0;
if(document.images){
if(navigator.userAgent.indexOf("Opera") != -1){
op = 1;
} else {
ie4 = (document.all && !document.getElementById);
nn4 = (document.layers);
ie5 = (document.all && document.getElementById);
nn6 = (document.addEventListener);
}
} else {
v3 = 1;
}
if(navigator.userAgent.indexOf("Mac") != -1){
isMac = 1;
}
// start the actual code for an alert
function alert_confirm(){
if(ie4 || ie5){
// if its IE4+ call the VB Script
retVal = makeMsgBox("Hi","how are you?",32,1,256,4096);
// which button was pressed?
if(retVal == 1) {alert("Ok pressed");}
else if (retVal == 2) {alert("Canceled");}
} else {
// else use a simple alert
alert("I am not IE");
}
}
// start the code for a prompt
function input_box(){
// only three args here
// Title, Question, Default answer
if(ie4 || ie5){
retVal = makeInputBox("Hi","How are you?","Fine, thanks");
} else {
retVal = prompt("How are you?","Fine, thanks");
}
if(retVal){
alert("Submitted");
} else {
alert("canceled");
}
}
// -->
</script>
This is the VB SCript called by the code above if IE is detected.
<script language="VBScript">
<!--
Function makeMsgBox(tit,mess,icon,buts,defs,mode)
butVal = icon + buts + defs + mode
makeMsgBox = MsgBox(mess,butVal,tit)
End Function
Function makeInputBox(title,question,default_answer)
makeInputBox = InputBox(title,question,default_answer)
End Function
// -->
</script>
Internet Explorer will call the VB Script while Netscape will execute the JavaScript. Below is a list of arguments each function takes. For example, if you look at the script above, you will find this line of code :
retVal = makeMsgBox("Hi","how are you?",32,1,256,4096);
You can change the numbers to create alerts, confirms or any type of popup using the numbers / arguments below.
The alert and confirm boxes take 4 arguments
Argument one : The Icon 0 = none
16 = X
32 = ?
48 = !
64 = i
Argument two : The buttons 0 = OK # standard alert
1 = OK CANCEL
2 = ABORT RETRY IGNORE
3 = YES NO CANCEL
4 = YES NO
5 = RETRY CANCEL
Argument three : 0 = First
In your popup, which 256 = Second
button should be selected 512 = Thrid
by default?
Argument four : 0 = Browser stalls
The system command will 4096 = All apps stall
stall either the browser
or the whole system until
the user responds
(either submits or cancels)
A value is returned
depending on which button
was pressed. So you could say
if(retVal == 3){do this}
else {do this}
Here are the return values OK = 1
Cancel = 2
Abort = 3
Retry = 4
Ignore = 5
Yes = 6
No = 7
Alert or Confirm box
Input box
|