This article creates a user-defined alertbox. effect:
Insert the following content into the JS file:
Code :
Copy code The Code is as follows: // JScript File
// Constants to define the title of the alert and button text.
VaR alert_title = "Oops! ";
VaR alert_button_text = "close ";
// Over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
If (document. getelementbyid ){
Window. Alert = function (txt ){
Createcustomalert (txt );
}
}
Function createcustomalert (txt ){
// Shortcut reference to the Document Object
D = document;
// If the modalcontainer object already exists in the Dom, bail out.
If (D. getelementbyid ("modalcontainer") return;
// Create the modalcontainer Div as a child of the Body element
Mobj = D. getelementsbytagname ("body") [0]. appendchild (D. createelement ("Div "));
Mobj. ID = "modalcontainer ";
// Make sure its as tall as it needs to be to overlay all the content on the page
Mobj. style. Height = document.doc umentelement. scrollheight + "PX ";
// Create the DIV that will be the alert
Alertobj = mobj. appendchild (D. createelement ("Div "));
Alertobj. ID = "alertbox ";
// MSIE doesnt treat position: Fixed correctly, so this compensates for positioning the alert
If (D. All &&! Window. Opera) alertobj. style. Top = document.doc umentelement. scrolltop + "PX ";
// Center the Alert box
Alertobj. style. Left = (d.doc umentelement. scrollwidth-alertobj. offsetwidth)/2 + "PX ";
// Create an H1 element as the title bar
H1 = alertobj. appendchild (D. createelement ("h1 "));
H1.appendchild (D. createtextnode (alert_title ));
// Create a paragraph element to contain the TXT argument
MSG = alertobj. appendchild (D. createelement ("p "));
MSG. innerhtml = txt;
// Create an anchor element to use as the confirmation button.
BTN = alertobj. appendchild (D. createelement (""));
BTN. ID = "closebtn ";
BTN. appendchild (D. createtextnode (alert_button_text ));
BTN. href = "#";
// Set up the onclick event to remove the alert when the anchor is clicked
BTN. onclick = function () {removecustomalert (); Return false ;}
// Removes the custom alert from the DOM function removecustomalert (){
// Document. getelementsbytagname ("body") [0]. removechild (document. getelementbyid ("modalcontainer "));
}
// Removes the custom alert from the DOM
Function removecustomalert ()
{
Document. getelementsbytagname ("body") [0]. removechild (document. getelementbyid ("modalcontainer "));
}
Paste the following code into your HTML head section. Copy code The Code is as follows: <SCRIPT type = "text/JavaScript" src = "include/customalertbox. js"> </SCRIPT>
<! -- Paste this code into your external style sheet or
CSS section of your HTML document -->
<Style type = "text/CSS">
# Modalcontainer {
Background-color: transparent;
Position: absolute;
Width: 100%;
Height: 100%;
Top: 0px;
Left: 0px;
Z-index: 10000;
}
# Alertbox {
Position: relative;
Width: 300px;
Min-Height: 100px;
Margin-top: 50px;
Border: 2px solid #000;
Background-color: # f2f5f6;
Background-image: url(alert.png );
Background-repeat: No-Repeat;
Background-position: 20px 30px;
}
# Modalcontainer> # alertbox {
Position: fixed;
}
# Alertbox H1 {
Margin: 0;
Font: bold 0.9em verdana, Arial;
Background-color: # 78919b;
Color: # FFF;
Border-bottom: 1px solid #000;
Padding: 2px 0 2px 5px;
}
# Alertbox P {
Font: 0.7em verdana, Arial;
Height: 50px;
Padding-left: 5px;
Margin-left: 55px;
}
# Alertbox # closebtn {
Display: block;
Position: relative;
Margin: 5px auto;
Padding: 3px;
Border: 1px solid #000;
Width: 70px;
Font: 0.7em verdana, Arial;
Text-transform: uppercase;
Text-align: center;
Color: # FFF;
Background-color: # 78919b;
Text-Decoration: none;
}
</Style>
Insert the following code into the body of your HTML document:Copy codeThe Code is as follows: <input type = "button" value = "test the alert" onclick = "alert ('this is a custom alert dialog that was created by overriding the window. alert method. '); ">