JS Pop-up Confirmation dialog box confirm (), alert (), prompt

Source: Internet
Author: User
Tags border color

Window.alert () Using warnings, hints, and accuracy

The code is as follows Copy Code

<script type= "Text/javascript" >
Function message ()
{
Alert ("This balloon is invoked through the OnLoad event.) ")
}
</script>

<body onload= "message ()" >

</body>

The return value of the Confirm method is true or false

The code is as follows Copy Code

<script language= "JavaScript" >
function Delete_confirm (e)
{
if (Event.srcElement.outerText = = "Delete")
{
Event.returnvalue = Confirm ("Delete is unrecoverable, are you sure you want to delete it?") ");
}
}
Document.onclick = delete_confirm;
</script>

<a href= "delete.aspx" onclick= "delete_confirm" > Delete </a>

Example 2

if (window.confirm) (' Are you sure you want to cancel the transaction? ')){
Alert ("OK");
return true;
}else{
Alert ("Cancel");
return false;
}


The prompt method also displays a modal message box. The user must close the message box before continuing the operation

var theresponse = window.prompt ("Welcome"?) "," Please enter your name here. ");

Example 1

The code is as follows Copy Code

<script language= "javascript" type= "Text/javascript" >
function Prom (HREF)
{
var name=prompt ("Input the name", "");


if (name== "admin")
{
var pwsd=prompt ("Input the password", "");
if (pwsd== "admin")
{
Alert ("Login ok ...");
Location.href=href;
}
Else{alert ("Error password");}
}
Else{alert ("error name");}
}
</script>

The above are all with JS own original, below we use JS to write a div+js pop-up prompt box, this is now commonly used

The code is as follows Copy Code

/*
JS Bullet Window Code: User experience excellent alert prompt effect
Pubdate:2011-05-26 10:15
E-mail:xpsem2010@gmail.com
Source: Small Jigsaw sem Blog
*/
Gets the element of the specified ID
function $xp (ID) {
return document.getElementById (ID);
}
Universal Event Acquisition Interface
function GetEvent ()
{
if (checkbrowser () = = ' IE ') return window.event;
Func=getevent.caller;
while (Func!=null)
{
var arg0 = func.arguments[0];
if (arg0)
{
if ((arg0.constructor==event | | arg0.constructor ==mouseevent)
|| (typeof (arg0) = = "Object" && arg0.preventdefault && arg0.stoppropagation))
{
return arg0;
}
}
Func=func.caller;
}
return null;
}
Alert
function Newalertbox (msg, time) {//time for vanishing times
var msgbg,msgcolor,bordercolor,content,posleft,postop;
Pop-up window settings
MSGBG = "#FFF"; Content background
Msgcolor = "#000"; Content Color
bordercolor = "#ccc"; Border color
Mask Background settings
Content = "Here is the expression picture address (HTML format)" + msg;
var swidth,sheight;
Swidth = screen.availwidth-20;//Prevent overflow
if (Screen.availheight > Document.body.scrollHeight) {
Sheight = Screen.availheight; Less than one screen
}else{
Sheight = Document.body.scrollHeight; More than one screen
}
Create a matte background
var maskobj = document.createelement ("div");
Maskobj.setattribute (' id ', ' maskdiv ');
MaskObj.style.position = "absolute";
MaskObj.style.top = "0";
MaskObj.style.left = "0";
MaskObj.style.background = "#fff";
MaskObj.style.filter = "Alpha (opacity=40);";
maskObj.style.opacity = "0.4";
MaskObj.style.width = swidth + "px";
MaskObj.style.height = sheight + "px";
MaskObj.style.zIndex = "10000";
Document.body.appendChild (Maskobj);
Create a pop-up window
var msgobj = document.createelement ("div")
Msgobj.setattribute ("id", "msgdiv");
MsgObj.style.position = "absolute";
var event = getEvent ();/DECLARE event
if (checkbrowser () = = ' IE ')
{
Posleft = Event.clientx + 10;
Postop = Event.clienty + document.documentElement.scrollTop;
}
Else
{
Posleft = Event.pagex + + "px";//ff to declare px
Postop = Event.pagey + + "px";
}
MsgObj.style.top = postop;
MsgObj.style.left = Posleft;
MsgObj.style.fontSize = "12px";
MsgObj.style.background = MSGBG;
MsgObj.style.border = "1px solid" + bordercolor;
MsgObj.style.zIndex = "10001";
Create content
var bodyobj = document.createelement ("div");
Bodyobj.setattribute ("id", "msgbody");
bodyObj.style.padding = "10px";
BodyObj.style.lineHeight = "1.5em";
var txt = document.createtextnode (content);
Bodyobj.appendchild (TXT);
bodyobj.innerhtml = content;
Build window
Document.body.appendChild (Msgobj);
$XP ("Msgdiv"). AppendChild (Bodyobj);
if (Time!= ') settimeout ("Closemsg ()", time);
else settimeout ("closemsg ()", 2000);//default two seconds to automatically disappear
return false;
}
removing objects
function Closemsg () {
Document.body.removeChild ($xp ("Maskdiv"));
$XP ("Msgdiv"). RemoveChild ($xp ("Msgbody"));
Document.body.removeChild ($xp ("Msgdiv"));
}
Determine browser type
function Checkbrowser () {
var cb = "Unknown";
if (window. ActiveXObject) {
CB = "IE";
}else if (Navigator.userAgent.toLowerCase (). IndexOf ("Firefox")!=-1) {
CB = "Firefox";
}else if ((typeof document.implementation!= "undefined") && (typeof document.implementation.createDocument "!=") Undefined ") && (typeof htmldocument!=" undefined ")) {
CB = "Mozilla";
}else if (Navigator.userAgent.toLowerCase (). IndexOf ("opera")!=-1) {
CB = "Opera";
}
return CB;
}

Call method

onclick= "Newalertbox (' Test alert prompt effect ', 3000);"

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.