Monday, 27 June 2011

Open div popup in a center of screen through JavaScript


function OpenDivCtrlInCenterOfScreen(objDiv, popupWidth, popupHeight)
{
    var availHeight = 0;
    var availWidth = 0;
   
    if(typeof(window.innerWidth) == 'number'){
        availHeight = window.innerHeight;
        availWidth = window.innerWidth;
    }else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
        availHeight = document.documentElement.clientHeight;
        availWidth = document.documentElement.clientWidth;
    }else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
        availHeight = document.body.clientHeight;
        availWidth = document.body.clientWidth;
    }

    var WinTop = (availHeight - popupHeight)/2;
    var WinLeft = (availWidth - popupWidth)/2;  
   
    if(WinTop < 0) WinTop = 0;
    if(WinLeft < 0) WinLeft = 0;   
   
    objDiv.style.top = WinTop + "px";
    objDiv.style.left = WinLeft + "px";
}

No comments:

Post a Comment