重写Alert

var isIE = navigator.appName.indexOf("Internet Explorer") != -1;
var oldAlert = window.alert;
window.alert = function (str) { 
var eSrc;
if (!isIE) {
eSrc = (document.all) ? window.event.srcElement : arguments[1];
} else {
eSrc = document.activeElement;
}
var shield = document.createElement("DIV");
shield.id = "shield";
shield.style.position = "absolute";
shield.style.left = "0px";
shield.style.top = "0px";
shield.style.width = "100%";
shield.style.height = ((document.documentElement.clientHeight > document.documentElement.scrollHeight) ? document.documentElement.clientHeight : document.documentElement.scrollHeight) + "px";
shield.style.background = "#FFF000";
shield.style.textAlign = "center";
shield.style.zIndex = "10000";
shield.style.filter = "alpha(opacity=0)";
shield.style.opacity = 0;
var alertFram = document.createElement("DIV");
alertFram.id = "alertFram";
alertFram.style.position = "absolute";
alertFram.style.left = "50%";
alertFram.style.top = "50%";
alertFram.style.marginLeft = "-225px";
alertFram.style.marginTop = -75 + document.documentElement.scrollTop + "px";
alertFram.style.width = "450px";
alertFram.style.height = "150px";
alertFram.style.background = "#ccc";
alertFram.style.textAlign = "center";
alertFram.style.lineHeight = "150px";
alertFram.style.zIndex = "10001";
strHtml = "<ul style=\"list-style:none;margin:0px;padding:0px;width:100%\">\n";
strHtml += " <li style=\"background:#DD828D;text-align:left;padding-left:20px;font-size:14px;font-weight:bold;height:25px;line-height:25px;border:1px solid #F9CADE;\">[系统提示]</li>\n";
strHtml += " <li style=\"background:#fff;text-align:center;font-size:12px;height:120px;line-height:120px;border-left:1px solid #F9CADE;border-right:1px solid #F9CADE;\">" + str + "</li>\n";
strHtml += " <li style=\"background:#FDEEF4;text-align:center;font-weight:bold;height:25px;line-height:25px; border:1px solid #F9CADE;\"><input type=\"button\" value=\"确 定\" id=\"do_OK\" onclick=\"doOk()\" /></li>\n";
strHtml += "</ul>\n";
alertFram.innerHTML = strHtml;
document.body.appendChild(alertFram);
document.body.appendChild(shield);
this.setOpacity = function (obj, opacity) {
if (opacity >= 1) opacity = opacity / 100;
try { obj.style.opacity = opacity; } catch (e) { }
try {
if (obj.filters.length > 0 && obj.filters("alpha")) {
obj.filters("alpha").opacity = opacity * 100;
} else {
obj.style.filter = "alpha(opacity=\"" + (opacity * 100) + "\")";
}
} catch (e) { }
}
var c = 0;
this.doAlpha = function () {
if (++c > 20) { clearInterval(ad); return 0; }
setOpacity(shield, c);
}
var ad = setInterval("doAlpha()", 1);
this.doOk = function () {
//alertFram.style.display = "none";
//shield.style.display = "none";
document.body.removeChild(alertFram);
document.body.removeChild(shield);
eSrc.focus();
document.body.onselectstart = function () { return true; }
document.body.oncontextmenu = function () { return true; }
}
document.getElementById("do_OK").focus();
eSrc.blur();
document.body.onselectstart = function () { return false; }
document.body.oncontextmenu = function () { return false; }
}

效果如下:

样式还可自己再改
}

时间: 2024-08-08 04:20:54

重写Alert的相关文章

重写alert弹出窗口样式

<script> window.alert = function(str)    {     var shield = document.createElement("DIV");     shield.id = "shield";     shield.style.position = "absolute";     shield.style.left = "0px";     shield.style.top

javascript 重写alert后如何找回来

//重写alertwindow.alert = function(){}; //调用找回alertfunction getAlert(){ var f = document.createElement("iframe"); f.style.cssText = "border:0;width:0;height:0;display:none"; document.body.appendChild(f); var d = f.contentWindow.document;

重写alert 方法(我胡汉三又回来了)

1 window.alert = function (txt) { 2 var shield = document.createElement("DIV"); 3 shield.id = "shield"; 4 shield.style.position = "absolute"; 5 shield.style.left = "0px"; 6 shield.style.top = "0px"; 7 shie

webview的使用

1 package com.example.webview; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.app.AlertDialog; 6 import android.content.DialogInterface; 7 import android.content.DialogInterface.OnClickListener; 8 import android.view.Me

司徒正美文章列表

由于本人对司徒正美文章的喜欢,特此整理文章列表如下. 一个带完整的RBAC授权系统的rails应用(第一部分)(司徒正美, 3年前, 12/6549) 一个带完整的RBAC授权系统的rails应用(第二部分)(司徒正美, 3年前, 1/1135) 随机生成十六进制颜色(司徒正美, 3年前, 0/340) ruby中的类变量与实例变量(司徒正美, 3年前, 0/231) ruby模拟多个构造器(司徒正美, 3年前, 0/153) ruby的实例方法(写方法,读方法与读写方法)(司徒正美, 3年前,

ios操作系统输入完成后,键盘没有弹下去的问题

在工作中遇到,ios系统,在输入完成后,弹出提示信息时,键盘没有弹下去,造成不好的用户体验.我的解决方案是,重写alert方法 JS 代码: window.alert=function(){ var shield = document.createElement("DIV"); shield.setAttribute("class","alert_dialog"); var strHtml=""; strHtml+='<

修改弹窗Style

只能自定一个弹窗样式 首先必须明白的一点是,alert只是一个方法,而这个方法内部是native code,这是我们无法修改的部分,而最终暴露的只有这个alert方法名字而已,你甚至拿不到alert的属性,因此要真正意义上的做到修改alert样式是不可行的. 有了以上这个条件基础,我们能做的只有重写alert方法,替换掉系统自带的alert方法. 一行代码替换alert方法 window.alert = function {}; 看到这,就有很多人已经明了了,首先你可以先写好一个假的弹窗样式,然

bootbox.js官方文档

简介 Bootbox.js是一个小型的JavaScript库,基于Bootstrap模态框开发,用于创建可编程的对话框. 不像原生的alert等对话框,所有的Bootstrap模态框生成的都是非阻塞事件.所以 在使用confirm()对话框时,请记住这一点,因为它不是本地确认对话框的替代. 任何取决于用户选择的代码都必须放在回调函数中. alert alert是只有单个按钮的对话框,按ESC键或单击关闭按钮可关闭对话框. bootbox.alert("Your message here…&quo

重写js alert

Window.prototype.alert = function(){ //创建一个大盒子 var box = document.createElement("div"); //创建一个关闭按钮 var button = document.createElement("button"); //定义一个对象保存样式 var boxName = { width:"500px", height:"180px", backgroun