Show top/left div corner in the middle of the browser screen?

I'm trying to show a "div" element to the user and i want it to show in the center of is browser screen.

I want to position the div top/left corner at the top/left/center of the user screen..

Anybody can help?
Thanks..
LVL 10
jabcocoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Repus024Commented:
Hello jabcoco,

Please look at the attached code snippet.  This should be a simple example of what you are trying to accomplish.  This takes the div and places it in the center of the user's browser window.

Please note that the div has "position:absolute" specified.  This is what allows it to be positioned anywhere dynamically.

Please let me know if you have trouble with this script or if it does not do what you are trying to accomplish.

Hope this helps!
<HTML>
<HEAD>
<TITLE></TITLE>
<script language="javascript">
function toggleDiv()
{
	var div = document.getElementById("floatDiv");
	if (div.style.display == "none")
	{
		div.style.display = "inline";
		var IE=document.all;
		var iXPos;
		var iYPos;
		if (!IE)
		{
			iXPos = self.innerWidth / 2;
			iYPos = self.innerHeight / 2;
			alert("self.innerWidth: " + self.innerWidth
			+ "\n self.innerHeight: " + self.innerHeight
			+ "\n iXPos: " + iXPos + "\n iYPos: " + iYPos);
		}
		else
		{
			iXPos = (document.body.clientWidth) / 2;
			iYPos = (document.body.clientHeight) / 2;
			alert("document.body.clientWidth: " + document.body.clientWidth
			+ "\n document.body.clientHeight: " + document.body.clientHeight
			+ "\n iXPos: " + iXPos + "\n iYPos: " + iYPos);
		}
		div.style.left=iXPos;
  		div.style.top=iYPos;
	}
	else
	{
		div.style.display = "none";
	}
}
</script>
</HEAD>
 
<BODY>
<form>
Form Field One: <input name="ff1" type="text"/>
<br/>
Form Field Two: <input name="ff2" type="text"/>
<br/><input type="button" value="Toggle Div" onclick="toggleDiv()"/>
<div id="floatDiv" style="position:absolute;display:none">
Floating Div
</div>
</form>
</BODY>
 
</HTML>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jabcocoAuthor Commented:
Work's perfectly!
Exactly what I'm looking for! :)

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.