咖喱碗糕`

`∧ ∧︵
ミ^ō^ミ灬)~ ~我是只可爱的狐狸```
http://freedom2130.cnblogs.com
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

重写alert,使用模态窗口强化alert。

Posted on 2007-06-27 10:33  刹那间我存在  阅读(1420)  评论(0编辑  收藏  举报

为什么要重写 alert

项目中经常碰到提示,提示的按钮文本多种多样,经常要根据需要显示。

 

演示地址

alert重写代码

<script>
 var winAlert=window.alert;
 window.alert=function(msg,dr){
 var reValue=null;
 if(dr){
  var obj=document.createElement("span");
      obj.innerHTML=msg;
      obj.style.position="absolute";
      obj.style.fontSize="9pt";
      obj.style.left="0px";
      obj.style.top="0px";
      obj.style.zIndex="9999";
      obj.style.visibility="hidden";
      document.body.appendChild(obj);
  var winWidth=obj.offsetWidth;
  var winHeight=obj.offsetHeight;
  dr.push(msg);
  var rev=window.showModalDialog("msg.htm?"+Math.random(),dr,"dialogWidth:"+(winWidth+20)+"px;dialogHeight:"+(winHeight+70)+"px");
  if(rev!=undefined)reValue=rev;
  }else{
  winAlert(msg);
  }
 return reValue;
 }
</script>

调用代码

 

<input type="button" value="测试" onClick="alert('你不能通过禁锢你的邻人来证明你的清白.锢你的邻人来证明你的清白锢你的邻人来证明你的',['确定','取消','重置','返回'])"/>

<input type="button" value="测试" onClick="alert('你不能通过禁锢你的邻人来证明你的清白.锢你的邻人来证明你的清白锢你的邻人来证明你的')"/>

源码下载

 

模态窗口显示页

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>提示信息                                                         </title>
<script language="javascript" type="text/javascript">
 function window.onload(){

   var bus=window.dialogArguments;
   if(bus.length>0){
    for(i=0;i<bus.length-1;i++){
    var btuObj=document.createElement("input");
        btuObj.type="button";
        btuObj.value=" "+bus[i]+" ";
        btuObj.style.marginLeft="2px";
        btuObj.style.marginRight="2px";
        btuObj.onclick=function(){window.returnValue=event.srcElement.value;window.close();}
        buttonbox.appendChild(btuObj);
       }

  
   }else{
    var btuObj=document.createElement("input");
        btuObj.type="button";
        btuObj.value=" 确定 ";
        btuObj.onclick=function(){window.returnValue=event.srcElement.value;window.close();}
        buttonbox.appendChild(btuObj);
   }
   msgbox.innerHTML=bus[bus.length-1];
 }
 
 function reSet(){
  //window.returnValue=Event.value;
  self.close();
 }
</script>
</head>

<body style="background-color:ButtonFace;margin:0px" scroll="no">

<div id="outbox">
<div style="font-size:9pt;line-height:12pt;padding:10px" id="msgbox">
</div>
<div style="padding-left:10px;padding-right:10px;text-align:center" id="buttonbox">
</div>
</div>
</body>

</html>