About This Code
Brief Description:
Center Div object
Notes Version:
R6.x, R5.x, R7.x
Last Modified:
01 Oct 2009
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
center_div(
"fUpload",
"500",
"500");
function center_div(divName,divWidth,divHeight)
{
var divObject=
document.
getElementById(divName);
var windowWidth = 630, winHeight = 460;
var left=
"100",top=
"100";
if (parseInt(
navigator.appVersion)>3)
{
if (
navigator.appName==
"Netscape")
{
windowWidth =
window.innerWidth;
winHeight =
window.innerHeight;
}
if (
navigator.appName.indexOf(
"Microsoft")!=-1)
{
windowWidth =
document.body.offsetWidth;
winHeight =
document.body.offsetHeight;
}
}
left=(windowWidth/2)-(divWidth/2);
top=(winHeight/2)-(divHeight/2);
divObject.style.left=left;
divObject.style.top=top;
divObject.style.width=divWidth;
divObject.style.height=divHeight;
}
Usage / Example
center_div function keeps your div object to center of the browser window with spcified width and height.