DHTML Lab: JavaScript Enhancement with VBScript | 8
JavaScript Enhancement with VBScript
the JavaScript functions
newAlert()
Our first function, newAlert(), is to be used in place of the alert() method:
IE4 = document.all; function newAlert(title,mess,icon,mods) { (IE4) ? makeMsgBox(title,mess,icon,0,0,mods) : alert(mess); }
If our script does not have browser detection variables already defined, we must define one to isolate Explorer 4:
IE4 = document.all;
newAlert() expects four arguments:
- title - a string to be displayed in the dialog title bar
- mess - a string to be displayed as the message to the user
- icon - an integer between 0 and 4, inclusive, denoting the icon to be displayed (refer to table on earlier page)
- mods - an integer, either 0 or 1, denoting the modality of the dialog (refer to table on earlier page)
An alert-type dialog is for providing information to the user, only. We, therefore, do not have an argument to denote button group or button default. The OK button is the only one we make available.
When newAlert() is called, it checks for Explorer. If Explorer is being used, it calls makeMsgBox(), our VBScript function, passing the four arguments, plus 0 as the fourth and fifth arguments of makeMsgBox(), since we can only use the OK button.
If Explorer is not the browser being used, the regular JavaScript alert() method is called.
Make your own newAlert() dialog using the form below. We don't check your input, so type carefully to avoid errors.
newConfirm()
Our newConfirm() function replaces JavaScript's confirm() method:
function newConfirm(title,mess,icon,defbut,mods) { if (IE4) { icon = (icon==0) ? 0 : 2; defbut = (defbut==0) ? 0 : 1; retVal = makeMsgBox(title,mess,icon,4,defbut,mods); retVal = (retVal==6); } else { retVal = confirm(mess); } return retVal; }
newConfirm() takes five arguments:
- title - a string to be displayed in the dialog title bar
- mess - a string to be displayed as the message to the user
- icon - an integer, either 0 or 1, denoting whether no icon or the Question icon is displayed.
- defbut - an integer between 0 and 1, inclusive, denoting the default button
- mods - an integer, either 0 or 1, denoting the modality of the dialog
A confirm dialog asks a Yes or No question, so we allow only the Yes/No button group. The icon can either be the Question icon or nothing. The default button, it follows, can be either the first or the second (0 or 1).
When newConfirm() is called, it first checks for Explorer. Then it processes the icon argument, to avoid errors. If the icon value is not 0, then 2 (the Question icon) is assumed. In the same way, defbuts is checked and if not zero (0), a value of 1 is assumed. The VBScript makeMsgBox() function is called, passing the required six arguments, and stores the return value from makeMsgBox() to the retVal variable. Notice that the fourth argument is hard-coded as 4 (the Yes/No button group) as this is the only option we allow for confirm dialogs.
We use confirm dialogs when we need a user decision. Our script then branches according to the decision. The JavaScript confirm() method returns a Boolean: true if the user clicked OK, and false if the user clicked Cancel. It is assumed that you are familiar with JavaScript confirm dialogs, and processing the value returned. Here, we must ensure that newConfirm() returns a Boolean. Since we are using the Yes/No button group, the return value can be either 6 (Yes) or 7 (No). We, therefore check for a makeMsgBox() return value of 6. If it is 6, retVal is true. Any other value makes retVal equal to false:
retVal = (retVal==6);
If Explorer is not the current browser, then the JavaScript confirm() method is called. In either case, the value returned by newConfirm(), retVal, is either true or false.
Make your own newConfirm() dialog using the form below. We don't check your input, so type carefully to avoid errors.
newPrompt()
The newPrompt() function replaces JavaScript's prompt() method:
function newPrompt(title,mess,def) { retVal = (IE4) ? makeInputBox(title,mess,def) : prompt(mess,def); return retVal; }
- title - a string to be displayed in the dialog title bar
- mess - a string to be displayed as the message to the user (a prompt)
- def - a string to be displayed in the input area as the default response
These are the same three arguments expected by makeInputBox(), so we simply pass them along if Explorer is being used. Otherwise, the prompt() method is called with the required two arguments. In either case, the return value, a string, is assigned to retVal.
Make your own newPrompt() dialog using the form below. We don't check your input, so type carefully to avoid errors.
IEBox()
The newAlert(), newConfirm() and newPrompt() functions produce results in any JavaScript browser. For Explorer 4 users, however, they produce an enhanced, more informative dialog, but with the same functionality.
For those working in Explorer-only environments, we've provided a fourth function IEBox(), which filters out other browsers and produces a dialog for Exlorer users only:
function IEBox(title,mess,icon,buts,defbut,mods) { retVal = (IE4) ? makeMsgBox(title,mess,icon,buts,defbut,mods) : null; return retVal; }
IEBox() gives full access to all dialog "look" variations possible with our makeMsgBox() function. It takes the same six arguments as makeMsgBox(), does no filtering and passes them on.
Make your own IEBox() dialog using the form below. We don't check your input, so type carefully to avoid errors.
We'll repeat the full script on our final code page, but first, let's wrap-up.
Produced by Peter Belesis and
All Rights Reserved. Legal Notices.Created: Nov. 18, 1998
Revised: Nov. 18, 1998
URL: https://www.webreference.com/dhtml/column22/js-vbNewjs.html