search
HomeWeb Front-endJS TutorialMessageBox_javascript tips for Js

I saw someone imitating alert on the forum and wrote one myself
Originally I wanted to imitate the MessageBox in winapi
But unfortunately js does not support blocking mode
The return value can only be asynchronous.
Support FF ie opera
DOCTYPE can be declared or not declared
There is a problem
The layer cannot be transparent in opera
It cannot be used for iframes in the page
In ie Unable to cover the selected

Copy the code The code is as follows:

nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



MessageBox演示
<script> <BR>/******************************************************************************************* <BR> * http://2lin.net <BR> * Email:caoailin@gmail.com <BR> * QQ:38062022 <BR> * Creation date: 2006-11-27 <BR> * 下面的内容可以拷贝到一个JS文件里面 <BR>*********************************************************************************************/ <br><br><BR>// 控制按钮常量 <BR>var MB_OK = 0; <BR>var MB_CANCEL = 1; <BR>var MB_OKCANCEL = 2; <BR>var MB_YES = 3; <BR>var MB_NO = 4; <BR>var MB_YESNO = 5; <BR>var MB_YESNOCANCEL = 6; <br><br>// 控制按钮文本 <BR>var MB_OK_TEXT = "确定"; <BR>var MB_CANCEL_TEXT = "取消"; <BR>var MB_YES_TEXT = " 是 "; <BR>var MB_NO_TEXT = " 否 "; <br><br>//提示图标 <BR>var MB_ICON = "http://2lin.net/image/information.gif"; <br><br><BR>//委托方法 <BR>var MB_OK_METHOD = null; <BR>var MB_CANCEL_METHOD = null; <BR>var MB_YES_METHOD = null; <BR>var MB_NO_METHOD = null; <BR>var MB_BACKCALL = null; <br><br>var MB_STR = '<style type="text/css"><!--' + <BR> 'body{margin:0px;}' + <BR> '.msgbox_title{background-color: #B1CDF3;height:25px;position:relative;font-size:14px;line-height:25px;padding-left:10px;border-bottom:1px solid #000;}' + <BR> '.msgbox_control{text-align:center;clear:both;height:28px;}' + <BR> '.msgbox_button{background-color: #B1CDF3;border:1px solid #003366;font-size:12px;line-height:20px;height:21px;}' + <BR> '.msgbox_content{padding:10px;float:left;line-height: 20px;}' + <BR> '.msgbox_icon{width: 50px;height: 50px;float: left;text-align: center;line-height:50px;padding-top:10px;}' + <BR> '.msgbox_mask{position:absolute;left:0px;top:0px;z-index:99999;background-color:#333333;width:100%;height:100%;}' + <BR> '.msgbox{background-color: #EFFAFE;position: absolute;height:auto;font-size:12px;top:50%;left:50%;border:1px solid #999999;}' + <BR> '-->' + <BR> '<div id="msgBoxMask" class="msgbox_mask" style="filter: alpha(opacity=0);display:none;"></script>
' +
            '
' +
            '
' +
            '
' +
            '
' +
            '
';

var Timer = null;

document.write(MB_STR);
var icon = new Image();
icon.src = MB_ICON;

/* Prompt dialog box
* Parameter 1: Prompt content
* Parameter 2: Prompt title
* Parameter 3: Icon path
* Parameter 4: Button type
*/

function MessageBox(){
var _content = arguments[0] || "This is a dialog box!";
   var _title   = arguments[1] || "提示";
   var _icon    = arguments[2] || MB_ICON;
   var _button  = arguments[3] || MB_OK;
   MB_BACKCALL  = arguments[4];

   var _iconStr = 'MessageBox_javascript tips for Js';
   var _btnStr  = '';

   
   switch(_button)
   {      
      case MB_CANCEL      : _btnStr = _btnStr.toFormatString("msgBoxBtnCancel", MB_CANCEL_TEXT); break;

      case MB_YES         : _btnStr = _btnStr.toFormatString("msgBoxBtnYes", MB_YES_TEXT); break;

      case MB_NO          : _btnStr = _btnStr.toFormatString("msgBoxBtnNo", MB_NO_TEXT); break;

      case MB_OKCANCEL    : 
           _btnStr = _btnStr.toFormatString("msgBoxBtnOk", MB_OK_TEXT)   "  " 
                      _btnStr.toFormatString("msgBoxBtnCancel", MB_CANCEL_TEXT); 
           break;

      case MB_YESNO       :
            _btnStr = _btnStr.toFormatString("msgBoxBtnYes", MB_YES_TEXT)   "  " 
                      _btnStr.toFormatString("msgBoxBtnNo", MB_NO_TEXT); 
            break;

      case MB_YESNOCANCEL :
            _btnStr = _btnStr.toFormatString("msgBoxBtnYes", MB_YES_TEXT)   "  " 
                      _btnStr.toFormatString("msgBoxBtnNo", MB_NO_TEXT)   "  " 
                      _btnStr.toFormatString("msgBoxBtnCancel", MB_CANCEL_TEXT); 
            break;

      default :  _btnStr = _btnStr.toFormatString("msgBoxBtnOk", MB_OK_TEXT);  break;  

   }
   //解决 FF 下会复位
   ScrollTop = GetBrowserDocument().scrollTop; 
   ScrollLeft = GetBrowserDocument().scrollLeft; 
   GetBrowserDocument().style.overflow = "hidden";
   GetBrowserDocument().scrollTop = ScrollTop;   
   GetBrowserDocument().scrollLeft = ScrollLeft; 

   $("msgBoxTitle").innerHTML = _title;
   $("msgBoxIcon").innerHTML = _iconStr.toFormatString(_icon);
   $("msgBoxContent").innerHTML = _content; 
   $("msgBoxControl").innerHTML =  _btnStr;

   OpacityValue = 0;
   $("msgBox").style.display = "";       
   try{$("msgBoxMask").filters("alpha").opacity = 0;}catch(e){};
   $("msgBoxMask").style.opacity = 0;
   $("msgBoxMask").style.display = "";
   $("msgBoxMask").style.height = GetBrowserDocument().scrollHeight   "px";
   $("msgBoxMask").style.width = GetBrowserDocument().scrollWidth   "px";

   Timer = setInterval("DoAlpha()",1);
   //设置位置   
   $("msgBox").style.width = "100%";   
   $("msgBox").style.width = ($("msgBoxIcon").offsetWidth   $("msgBoxContent").offsetWidth   2)   "px";

   $("msgBox").style.marginTop = (-$("msgBox").offsetHeight/2   GetBrowserDocument().scrollTop)   "px";
   $("msgBox").style.marginLeft = (-$("msgBox").offsetWidth/2   GetBrowserDocument().scrollLeft)   "px";   

   if(_button == MB_OK || _button == MB_OKCANCEL){
     $("msgBoxBtnOk").focus();
   }else if(_button == MB_YES || _button == MB_YESNO || _button == MB_YESNOCANCEL){
       $("msgBoxBtnYes").focus();
   }  

}

var OpacityValue = 0;
var ScrollTop = 0;
var ScrollLeft = 0;

function GetBrowserDocument()
{
   var _dcw = document.documentElement.clientHeight;
   var _dow = document.documentElement.offsetHeight;
   var _bcw = document.body.clientHeight;
   var _bow = document.body.offsetHeight;

   if(_dcw == 0) return document.body;
   if(_dcw == _dow) return document.documentElement;

   if(_bcw == _bow && _dcw != 0) 
     return document.documentElement;
   else
     return document.body;
}

function SetOpacity(obj,opacity){
        if(opacity >=1 ) opacity = opacity / 100;    

        try{obj.style.opacity = opacity; }catch(e){}

        try{ 
            if(obj.filters){
                obj.filters("alpha").opacity = opacity * 100;
            }

        }catch(e){}
}

function DoAlpha(){
    if (OpacityValue > 20){clearInterval(Timer);return 0;}
    OpacityValue  = 5;    
    SetOpacity($("msgBoxMask"),OpacityValue);
}

function MBMethod(obj)
{   
   switch(obj.id)
   {
      case "msgBoxBtnOk" : if(MB_BACKCALL) {MB_BACKCALL(MB_OK);} else {if(MB_OK_METHOD) MB_OK_METHOD();} break;
      case "msgBoxBtnCancel" : if(MB_BACKCALL) {MB_BACKCALL(MB_CANCEL);} else {if(MB_CANCEL_METHOD) MB_CANCEL_METHOD();} break;
      case "msgBoxBtnYes" : if(MB_BACKCALL) {MB_BACKCALL(MB_YES);} else {if(MB_YES_METHOD) MB_YES_METHOD();} break;
      case "msgBoxBtnNo" : if(MB_BACKCALL) {MB_BACKCALL(MB_NO);} else {if(MB_NO_METHOD) MB_NO_METHOD();} break;
   }   

   MB_OK_METHOD = null;
   MB_CANCEL_METHOD = null;
   MB_YES_METHOD = null;
   MB_NO_METHOD = null;
   MB_BACKCALL = null;

   MB_OK_TEXT = "确定";
   MB_CANCEL_TEXT = "取消";
   MB_YES_TEXT = " 是 ";
   MB_NO_TEXT = " 否 ";

   $("msgBox").style.display = "none";  
   $("msgBoxMask").style.display = "none";     
   GetBrowserDocument().style.overflow = ""; 
   GetBrowserDocument().scrollTop = ScrollTop;
   GetBrowserDocument().scrollLeft = ScrollLeft; 
}

String.prototype.toFormatString = function(){  
   var _str = this;
   for(var i = 0; i       _str = eval("_str.replace(/\{"  i  "\}/ig,'"   arguments[i]   "')");
   }
   return _str;
}

function $(obj){
   return document.getElementById(obj);
}
///////////////////////////////////////////////////////////////////////////////////////

<script> <BR>function test() <BR>{ <BR>   var _msg = "<font color=red><b>演示:<br/>普通对话框!"; <BR>   MessageBox(_msg); <BR>} <br><br>function test1() <BR>{ <BR>  MB_OK_METHOD = function(){alert('你按了OK');} <BR>  MB_YES_METHOD = function(){alert('你按了YES');} <BR>  MB_NO_METHOD = function(){alert('你按了NO');} <BR>  MB_CANCEL_METHOD = function(){alert('你按了CANCEL');}  <br><br>  var _msg = "<font color=red><b>演示:<br/>调用对话框。是、否、取消"; <BR>  MessageBox(_msg,"演示",null,MB_YESNOCANCEL);   <BR>} <br><br>function test2() <BR>{  <BR>  var _msg = "<font color=red><b>演示:<br/>调用对话框。是、否、取消"; <BR>  MessageBox(_msg,"演示",MB_ICON,MB_YESNOCANCEL,callback); <BR>} <br><br>function test4() <BR>{  <BR>  var _msg = "<font color=red><b>演示:<br/>调用对话框。OK, Cancel"; <BR> MessageBox(_msg,"Demo",MB_ICON,MB_OKCANCEL,callback); <BR>} <br><br>function callback(value) <BR>{ <BR> switch(value) <BR> { <BR> case MB_OK : alert('You pressed OK'); break; <BR> case MB_YES : alert('You pressed YES'); break; <BR> case MB_NO : alert('You pressed NO'); break; <BR> case MB_CANCEL : alert('You pressed CANCEL'); break; <BR> } <BR>} <br><br>function test3() <BR>{ <BR> MB_YES_TEXT = "Demo one"; <BR> MB_NO_TEXT = "Demo two"; <BR> MB_CANCEL_TEXT = "Demo three"; <BR> var _msg = "<font color=red><b>Demo:</ b><br/>This is a custom dialog box<br/> <font color=green>MB_YES_TEXT MB_NO_TEXT MB_CANCEL_TEXT MB_OK_TEXT"; <BR> MessageBox(_msg, "Demo",MB_ICON,MB_YESNOCANCEL,callback); <BR>} <br><br></script>











                                                                    ;td align="center">Callback Demo 3
                                                                align="center">Customized demo



Normal demo ;
Callback Demo 1
                                                                        🎜>
Callback Demo 2

a>
td>




Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools