|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAlert boxes in JavaScript are notoriously unforgiving when it comes to designing a user interface. You cannot add images, change the background color, or otherwise "theme" the dialog to match the style of your web page or web application. Fortunately, JavaScript in conjunction with the Document Object Model (DOM) is there to help us achieve an otherwise unobtainable goal. BackgroundThis is my second attempt at creating a JavaScript alert box. My first attempt was a few years ago. The code was buggy and difficult to use, so I put it on the backburner with the intention of doing a complete rewrite. I recently began upgrading the web application that made use of the old code, and decided that it was time to breath life into the old code. There are a number of similar alert boxes on the web, but most are part of larger projects, or make use of external libraries. Using the CodeTo use the custom alert/dialog boxes, you must add the JavaScript source to your header, as well as the .css files that determine the "skin" the boxes will use. <link rel="Stylesheet" href="default.css" type="text/css" media="screen" />
<script type="text/javascript" src="DOMAlert.js"></script>
If you fail to include a reference to a stylesheet, the .js file will look for default.css in the local path. Once you have added your .js and .css files, you can create a new alert box by using the following constructor: var msg = new DOMAlert(
{
title: 'Title',
text: 'Alert Message',
skin: 'default',
width: 300,
height: 300,
ok: {value: true, text: 'Yes', onclick: showValue},
cancel: {value: false, text: 'No', onclick: showValue }
});
The constructor takes only one argument, an object containing one or more of the following properties:
MethodsThe following methods are currently supported:
CallbackThe callbacks for the OK and Cancel buttons can either be references to an existing function or a closure. For pre-existing functions, two objects are passed as arguments, a reference to the alert box that the function showValue(sender, value)
{
//sender is the alert box that the event was called from
//value is the value that was passed from the pressed button
}
Skins/ThemesThe .js file contains little to no information regarding the design of the alert box, only the layout. Since users may have different requirements in the design on a per alert box basis, and to separate design from code, the design information resides in .css files. If no skin information is provided, the code looks for a 'default.css' file to be located in the current directory. Each CSS name needs to be prefixed with the name of the skin you wish to use. So, if your theme is named ' This allows you to include all your skin information in one .css file, or in multiple ones. It's entirely up to you. However, some aspects like position or width may be overwritten in the .js file. The following style names can be used:
BugsIE6. Do I really need to say any more?
Points of InterestThe ' History
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||