You can customize only one pop-up window style
The first thing to understand is that alert is just a method, and this method is native code inside, which is the part we can't modify, and the only way to expose it is the alert method name, and you can't even get the alert property. Therefore, it is not feasible to modify the alert style in real sense.
With the above conditions in place, all we can do is rewrite the alert method and replace the alert method that comes with the system.
Replace Alert method with one line of code
Window.alert = function {};
See this, there are a lot of people already understand, first you can write a fake pop-up window style, and then show it in this way, this is the HTML CSS JS three-side changes to achieve the effect, you can also all JavaScript to complete the structure of the operation of the style, Today our example is all through JS to complete the structure style control.
Clear the basic way is to replace the alert method, then start to further consider the implementation steps.
steps to implement a custom alert method
Determine pop-up style HTML structure--structure insert--style control--click OK to delete structure
Determine structure
<div>
<p>alert content </p>
<div> OK </div>
</div>
using the JS processing structure
So here's the code:
Window.alert = alert;
function alert (data) {
var a = document.createelement ("div"),//create outermost label
p = document.createelement ("P"),//create P label for display content
BTN = document.createelement ("div"),//Create button label
Textnode = document.createTextNode (data),//Create a text node
Btntext = document.createTextNode ("OK");//Create Text node
Internal structure nesting
P.appendchild (Textnode);//Insert the content node you want to display in the P tag
Btn.appendchild (Btntext);//Insert the button text into the button label
A.appendchild (P);//insert P tag into the perimeter div
A.appendchild (BTN);//Insert the button into the perimeter div
Overall display in the page
document.getElementsByTagName ("Body") [0].appendchild (a);
}
Why not use Window.alert = function {} Directly here, the method is that the pre-compilation is not assigned to Window.alert, if written in this way, in Window.alert = function {} The previous call to alert will also be the system's own alert.
When you first call alert, if you do not pass parameters will be error, then we need a data judgment, Textnode = document.createTextNode (data) can be changed to Textnode = document.createTextNode (Data data: "")
optimize the current JS code
Next we will find that we also lack the style and determine the shutdown function. It is entirely possible to bind with DOM0-level events. The code quickly becomes like this:
Window.alert = alert;
function alert (data) {
var a = document.createelement ("div"),
p = document.createelement ("P"),
BTN = document.createelement ("div"),
Textnode = document.createTextNode (Data data: ""),
Btntext = document.createTextNode ("OK");
Internal structure nesting
P.appendchild (Textnode);
Btn.appendchild (Btntext);
A.appendchild (P);
A.appendchild (BTN);
Overall display in the page
document.getElementsByTagName ("Body") [0].appendchild (a);
OK binding fixed point Click event Delete label
Btn.onclick = function {
A.parentnode.removechild (a);
}
}
Style control
There is one last style control problem left. In order to be more beautiful and convenient to write, we need to write a method to control the style.
function css (targetobj, cssobj) {
for (var i in cssobj) {
Targetobj.style[i] = Cssobj[i];
}
}
This way we can control the style as follows:
CSS (target, {
"Background-color": "Red",
"Text-align": "Center",
})
Wait Wait, it's not over yet.
After the test found, such as on the style of writing, IE8 below there are rendering problems, then we switch to Csstext. Therefore, the style control code is changed to-
function css (targetobj, cssobj) {
var str = targetobj.getattribute ("style")? Targetobj.getattribute ("style"): "";
for (var i in cssobj) {
str + = i + ":" + cssobj[i] + ";";
}
TargetObj.style.cssText = str;
}
This will solve the IE8 embarrassment, but also brings other problems, string concatenation, there may be duplicate attributes, so if you need to do a generic, you also need to check the string, but for this example is fully sufficient.
completed version of alert function
Our demo is like this (style or self-tuning, the following style is just a demonstration):
<! DOCTYPE html>
<meta charset= "UTF-8" >
<TITLE>HTML5 Academy-Alert</title>
<body>
<script>
Window.alert = alert;
function alert (data) {
var a = document.createelement ("div"),
p = document.createelement ("P"),
BTN = document.createelement ("div"),
Textnode = document.createTextNode (Data data: ""),
Btntext = document.createTextNode ("OK");
Control style
CSS (A, {
"Position": "Fixed",
"Left": "0",
"Right": "0",
"Top": "20%",
"width": "100px",
"Margin": "0 Auto",
"Background-color": "#f00",
"Font-size": "20px",
"Text-align": "Center"
});
CSS (BTN, {
"Background": "Blue",
})
Internal structure nesting
P.appendchild (Textnode);
Btn.appendchild (Btntext);
A.appendchild (P);
A.appendchild (BTN);
Overall display in the page
document.getElementsByTagName ("Body") [0].appendchild (a);
OK binding fixed point Click event Delete label
Btn.onclick = function {
A.parentnode.removechild (a);
}
}
function css (targetobj, cssobj) {
var str = targetobj.getattribute ("style")? Targetobj.getattribute ("style"): "";
for (var i in cssobj) {
str + = i + ":" + cssobj[i] + ";";
}
TargetObj.style.cssText = str;
}
alert (123);
</script>
</body>
Finally, this example applies the knowledge point, Dom operation, pre-compilation period and execution, for-in cycle, csstext. The main purpose of this article is to guide the idea, no matter what the project, the idea is very important, to know how to adapt, if you want to modify the alert style through certain properties, you want to break the head can not think of, all the effect of the implementation method is not unique, just need a you want the alert style, can "fake", The rational use of "fake" method, you can make your effect write more easily than others.
Modify pop-up style