Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

How to show div popup to the center of page.my code as below

JavaScript
<style>
#modalContainer {
	background-color:transparent;
	position:absolute;
	width:100%;
	height:100%;
	top:0px;
	left:0px;
	z-index:10000;
	background-image:url(tp.png); } 
#alertBox {
	position:relative;
	width:300px;
	min-height:100px;
	border:2px solid #999;
	background-color:#F2F5F6;
	background-image:url(alert.png);
	background-repeat:no-repeat;
	background-position:20px 30px;
} 
#modalContainer > #alertBox {
	position:fixed;
} 
#alertBox h1 {
	margin:1px;
	font:bold 12px verdana,arial;
	background-color:#367cab;
	color:#FFF;
	border-bottom:1px solid #000;
	padding:2px 0 2px 5px;
} 
#alertBox p {
	font:0.7em verdana,arial;
	height:50px;
	padding-left:1px;
	margin-left:8px;
} 
</style>
<script>
 var ALERT_TITLE = "Message";
 var ALERT_BUTTON_TEXT = "Ok";
 var DeletionConfirmation = 0;
 var isMozilla;
 var objDiv = null;
 var over = false; 
if(document.getElementById) {	
		window.alert = function(txt) 
{ createCustomAlert(txt); } 
	
}
function createCustomAlert(txt) {
      Initialize();
	d = document; 
	if(d.getElementById("modalContainer")) return; 
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	mObj.style.height = d.documentElement.scrollHeight + "px";
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";      
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";
	alertObj.style.visiblity="visible"; 
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));
      h1.onmouseover= function() {over=true;return false;}
      h1.onmouseout= function() {over=false;return false;}
      h1.style.cursor="move";
	msg = alertObj.appendChild(d.createElement("p"));
	//msg.appendChild(d.createTextNode(txt));
	msg.innerHTML = txt;
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "#";
	btn.focus();
	btn.onclick = function() { removeCustomAlert();return false; } 
	alertObj.style.display = "block";      
	
}
 
function removeCustomAlert() {    
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}
function AlertMsg()
{
  alert('This is a custom alert dialog that was created by over-riding the window.alert method.');   
}
function Initialize()
{  
    isMozilla = (document.all) ? 0 : 1;
    if (isMozilla) {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    }
    document.onmousedown = MouseDown;
    document.onmousemove = MouseMove;
    document.onmouseup = MouseUp;
}
function MouseDown(e) {
    if (over) {
        if (isMozilla) {
            objDiv = document.getElementById('alertBox');
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else {
            objDiv = document.getElementById('alertBox');
            objDiv = objDiv.style;
            X = event.offsetX;
            Y = event.offsetY;
        }
    }
}
function MouseMove(e) {   
    if (objDiv) {
        if (isMozilla) {
            objDiv.style.top = (e.pageY - Y) + 'px';
            objDiv.style.left = (e.pageX - X) + 'px';
            return false;
        }
        else {          
            objDiv.pixelLeft = event.clientX - X + document.body.scrollLeft;
            objDiv.pixelTop = event.clientY - Y + document.body.scrollTop;
            return false;
 
        }
    }
}
function MouseUp() {
    objDiv = null;
}
</script>
<body> 
<input type="button" value = "Alert"  onclick="AlertMsg()">
</body>
</html>


Thanks....
Posted
Updated 14-Jun-13 2:53am
v3
Comments
Sunasara Imdadhusen 14-Jun-13 7:50am    
So what is your problem?
hasbina 14-Jun-13 8:13am    
@Sunasara
now showing it on top of page.how to make to center of page.
Thanks7872 14-Jun-13 7:51am    
Try margin:0 auto 0 auto; in #alertbox
hasbina 14-Jun-13 8:04am    
@Rohan
i added margin:0 auto 0 auto in #alertbox,but no change..

Refer to this one.I hope it would be helpful

Using jQuery to center a DIV on the screen[^]

popup in center of screen css[^][Try each link one by one]

Also try position:fixed
 
Share this answer
 
Hasbina,
If I were you I would be using jquery as it handles cross browser syntax. Anyways there is an extension you can usefor this purpose. But you will have to include jquery file. In the head section of your page add
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>

At the end of your script add:
JavaScript
jQuery.fn.center = function () {
this.css("position", "absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) +
$(window).scrollLeft()) + "px");
return this;
}


Now, in you function replace

JavaScript
if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

with
JavaScript
$(alertObj).center();
 
Share this answer
 
v2
Comments
Zafar Sultan 14-Jun-13 9:14am    
It is working on my PC. I have tested it on IE9
hasbina 14-Jun-13 9:17am    
@Zafar

sorry sir.it is working...thanks youuu...thanks a lot....thank you sir..
Zafar Sultan 14-Jun-13 9:21am    
Welcome. But yes, it is not working in FF 21.0
But you have the idea. Just add an else block and set the position for FF in it.
hasbina 15-Jun-13 5:21am    
sir, how to set it in FF 21.0

Thanks..
hasbina 15-Jun-13 5:48am    
@Zafar
sir, its not working in all other browsers except ie.how to solve ?

thankss..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900