422,946 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,946 IT Pros & Developers. It's quick & easy.

center a css popup in the browser using javascript

P: 5
What I'm trying to do is to display a loading screen while an ajax request executes. I have a page that is fairly long with several tables worth of data and the sorting/pagination of each table is handled by ajax requests.

I'd like to have a css div that is displayed in the center of the browser window when a user selects an operation that kicks off an ajax request.

What I have thus far is a div that shows up in the center of the "unscrolled" browser window when needed (by unscrolled I mean that if the user were to first hit the page and activate a link in the visible portion of the page - not one found via scrolling down - the div would appear centered in that visible area). The problem that I'm having is that, if the user scrolls down and selects a link the kicks off a request, the div still appears centered on the "top" portion of the page (the original, "unscrolled" portion). I want the pop up div to be displayed in the center of the "visible" area of the browser. Here's the javascript I have thus far:

Expand|Select|Wrap|Line Numbers
  1. function showIndicator(){
  2.     var availHeight;
  3.     var availWidth;
  4.  
  5.     if(typeof(window.innerWidth) == 'number'){
  6.         availHeight = window.innerHeight;
  7.         availWidth = window.innerWidth;
  8.     }else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
  9.         availHeight = document.documentElement.clientHeight;
  10.         availWidth = document.documentElement.clientWidth;
  11.     }else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
  12.         availHeight = document.body.clientHeight;
  13.         availWidth = document.body.clientWidth;
  14.     }
  15.  
  16.     var indicatorWidth = 50;
  17.     var indicatorHeight = 50;
  18.     var left = (availWidth/2) - (indicatorWidth/2);
  19.     var top = (availHeight/2) - (indicatorHeight/2);
  20.     var indicator = document.getElementById('indicator');
  21.     indicator.style.border="1px solid gray";
  22.     indicator.style.background="white";
  23.     indicator.style.position="absolute";
  24.     indicator.style.zIndex="999";
  25.     indicator.style.top=top+"px";
  26.     indicator.style.left=left+"px";
  27.     indicator.style.display="block";
  28. }
  29.  
I hope I was able to convey my intent and my problem - I'm quite new to javascript/dhtml/ajax so I'm mainly hacking away in the dark here.
-I've only tested this in firefox 2 and ie 6 thus far.
Thanks very much!
Apr 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: 5
cmo
well, ended up figuring this one out after some more hacking,
added the following:
Expand|Select|Wrap|Line Numbers
  1. var yOffset
  2. if (self.pageYOffset){// all except Explorer
  3.     yOffset = self.pageYOffset;
  4. }
  5. else if (document.documentElement && document.documentElement.scrollTop)// Explorer 6 Strict{
  6.     yOffset = document.documentElement.scrollTop;
  7. }
  8. else if (document.body){ // all other Explorers
  9.     y = document.body.scrollTop;
  10. }
  11. ...
  12. var top = ((availHeight/2)+yOffset) - (indicatorHeight/2);
  13. ...
  14.  
and that seems to work just fine.
Apr 2 '07 #2

P: 54
well, ended up figuring this one out after some more hacking,
added the following:
Expand|Select|Wrap|Line Numbers
  1. var yOffset
  2. if (self.pageYOffset){// all except Explorer
  3.     yOffset = self.pageYOffset;
  4. }
  5. else if (document.documentElement && document.documentElement.scrollTop)// Explorer 6 Strict{
  6.     yOffset = document.documentElement.scrollTop;
  7. }
  8. else if (document.body){ // all other Explorers
  9.     y = document.body.scrollTop;
  10. }
  11. ...
  12. var top = ((availHeight/2)+yOffset) - (indicatorHeight/2);
  13. ...
  14.  
and that seems to work just fine.
I was doing more or less that some days ago
The code i used was something like

Expand|Select|Wrap|Line Numbers
  1. var x = self.pageYOffset ? self.pageXOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollLeft : document.body ? document.body.scrollLeft : null;
  2.  
  3. var y = self.pageYOffset ? self.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body ? document.body.scrollTop : null;
  4.  
Apr 3 '07 #3

P: 2
Hai im looking for a css inner popup that need to be place along to the window adjustments,

for example : when we right click the mouse on screen where ever the mouse pointer is there option will display accourding to that.

look at this code and position the displaying popup according to the button click

[HTML]<html>
<head>
<title>Inner Popup using CSS</title>
<style type="text/css">
.popup
{
position:absolute; left:0; top:0; width:400px;
border-style:solid;
border-width:1px;
border-color:blue;
background-color:yellow;
padding:5px;
color:red;
font-family:Arial;
font-weight:bold;
font-size:10pt;
z-index:2;
visibility:hidden;
}
</style>
<script language="JavaScript">
function ShowPop(id)
{
document.getElementById(id).style.visibility = "visible";
}
function HidePop(id)
{
document.getElementById(id).style.visibility = "hidden";
}
</script>
</head>
<body>
<table width="100%" class="frm" border=1>
<tr colspan=3>Inner POPUp using CSS
</tr>
<tr>
<td>
<span style="position:relative; background-color:#33d;">
<span id="example" class="popup">
<pre>&lt;script language="JavaScript"&gt;
function ShowPop(id)
{
document.getElementById(id).style.visibility = "visible";
}
function HidePop(id)
{
document.getElementById(id).style.visibility = "hidden";
}
&lt;/script&gt;
<input id="butt" type="button" onClick="HidePop('example');" value="Close" /></pre>
</span>
<input id="butt" type="button" onClick="javascript:void(0);ShowPop('example');" value="click"/></span>
</td>
<td>
<span style="position:relative; background-color:#33d;">
<span id="example" class="popup">
<pre>&lt;script language="JavaScript"&gt;
function ShowPop(id)
{
document.getElementById(id).style.visibility = "visible";
}
function HidePop(id)
{
document.getElementById(id).style.visibility = "hidden";
}
&lt;/script&gt;
<input id="butt" type="button" onClick="HidePop('example');" value="Close" /></pre>
</span>
<input id="butt" type="button" onClick="javascript:void(0);ShowPop('example');" value="Click" /></span>
</td><td>
<span style="position:relative; background-color:#33d;">
<span id="example" class="popup">
<pre>&lt;script language="JavaScript"&gt;
function ShowPop(id)
{
document.getElementById(id).style.visibility = "visible";
}
function HidePop(id)
{
document.getElementById(id).style.visibility = "hidden";
}
&lt;/script&gt;
<input id="butt" type="button" onClick="HidePop('example');" value="Close" /></pre>
</span>
<input id="butt" type="button" onClick="javascript:void(0);ShowPop('example');" value="Click" /></span>
</td>
</tr>
</table>
</body>
</html>[/HTML]

plz find the solution for this, i have to give this to my programmers, who are not able to fix this
Jan 9 '08 #4

acoder
Expert Mod 15k+
P: 16,027
See if you find this article useful.
Jan 10 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.