Custom alert box formatted and controlled by JS+CSS


/ Published in: JavaScript
Save to your folder(s)

alert box styled with color combination's


Copy this code and paste it in your HTML
  1. <!--<Developed and experimented by -----SlayerOffice------- >-->
  2.  
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6. <head>
  7. <title>ustom alert demonstration</title>
  8. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  9. <style type="text/css">
  10.  
  11. #modalContainer {
  12. background-color:transparent;
  13. position:absolute;
  14. width:100%;
  15. height:100%;
  16. top:0px;
  17. left:0px;
  18. z-index:10000;
  19. background-image:url(tp.png); /* required by MSIE to prevent actions on lower z-index elements */
  20. }
  21.  
  22. #alertBox {
  23. position:relative;
  24. width:300px;
  25. min-height:100px;
  26. margin-top:50px;
  27. border:2px solid #000;
  28. background-color:#F2F5F6;
  29. background-image:url(alert.png);
  30. background-repeat:no-repeat;
  31. background-position:20px 30px;
  32. }
  33.  
  34. #modalContainer > #alertBox {
  35. position:fixed;
  36. }
  37.  
  38. #alertBox h1 {
  39. margin:0;
  40. font:bold 0.9em verdana,arial;
  41. background-color:#78919B;
  42. color:#FFF;
  43. border-bottom:1px solid #000;
  44. padding:2px 0 2px 5px;
  45. }
  46.  
  47. #alertBox p {
  48. font:0.7em verdana,arial;
  49. height:50px;
  50. padding-left:5px;
  51. margin-left:55px;
  52. }
  53.  
  54. #alertBox #closeBtn {
  55. display:block;
  56. position:relative;
  57. margin:5px auto;
  58. padding:3px;
  59. border:2px solid #000;
  60. width:70px;
  61. font:0.7em verdana,arial;
  62. text-transform:uppercase;
  63. text-align:center;
  64. color:#FFF;
  65. background-color:#78919B;
  66. text-decoration:none;
  67. }
  68.  
  69. /* unrelated styles */
  70.  
  71. #mContainer {
  72. position:relative;
  73. width:600px;
  74. margin:auto;
  75. padding:5px;
  76. border-top:2px solid #000;
  77. border-bottom:2px solid #000;
  78. font:0.7em verdana,arial;
  79. }
  80.  
  81. h1,h2 {
  82. margin:0;
  83. padding:4px;
  84. font:bold 1.5em verdana;
  85. border-bottom:1px solid #000;
  86. }
  87.  
  88. code {
  89. font-size:1.2em;
  90. color:#069;
  91. }
  92.  
  93. #credits {
  94. position:relative;
  95. margin:25px auto 0px auto;
  96. width:350px;
  97. font:0.7em verdana;
  98. border-top:1px solid #000;
  99. border-bottom:1px solid #000;
  100. height:90px;
  101. padding-top:4px;
  102. }
  103.  
  104. #credits img {
  105. float:left;
  106. margin:5px 10px 5px 0px;
  107. border:1px solid #000000;
  108. width:80px;
  109. height:79px;
  110. }
  111.  
  112. .important {
  113. background-color:#F5FCC8;
  114. padding:2px;
  115. }
  116.  
  117. code span {
  118. color:green;
  119. }
  120. </style>
  121. <script type="text/javascript">
  122.  
  123. var ALERT_TITLE = "Oops!";
  124. var ALERT_BUTTON_TEXT = "Ok";
  125.  
  126. if(document.getElementById) {
  127. window.alert = function(txt) {
  128. createCustomAlert(txt);
  129. }
  130. }
  131.  
  132. function createCustomAlert(txt) {
  133. d = document;
  134.  
  135. if(d.getElementById("modalContainer")) return;
  136.  
  137. mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
  138. mObj.id = "modalContainer";
  139. mObj.style.height = d.documentElement.scrollHeight + "px";
  140.  
  141. alertObj = mObj.appendChild(d.createElement("div"));
  142. alertObj.id = "alertBox";
  143. if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
  144. alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";
  145. alertObj.style.visiblity="visible";
  146.  
  147. h1 = alertObj.appendChild(d.createElement("h1"));
  148. h1.appendChild(d.createTextNode(ALERT_TITLE));
  149.  
  150. msg = alertObj.appendChild(d.createElement("p"));
  151. //msg.appendChild(d.createTextNode(txt));
  152. msg.innerHTML = txt;
  153.  
  154. btn = alertObj.appendChild(d.createElement("a"));
  155. btn.id = "closeBtn";
  156. btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
  157. btn.href = "#";
  158. btn.focus();
  159. btn.onclick = function() { removeCustomAlert();return false; }
  160.  
  161. alertObj.style.display = "block";
  162.  
  163. }
  164.  
  165. function removeCustomAlert() {
  166. document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
  167. }
  168. </script>
  169.  
  170. <body>
  171.  
  172. <input type="button" value = "Test the alert" onclick="alert('This is a custom alert dialog that was created by over-riding the window.alert method.');" />
  173.  
  174. </body>
  175. </html>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.