Js custom AlertBox (Information prompt box) _ javascript skills

Source: Internet
Author: User
This is a translation. The implementation code of the instance analysis and custom information prompt box is as follows:

Insert the following code into the js file:

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 p as a child of the BODY element
MObj = d. getElementsByTagName ("body") [0]. appendChild (d. createElement ("p "));
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 ("p "));
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.

The Code is as follows:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.