Link to home
Start Free TrialLog in
Avatar of GSMadMan
GSMadManFlag for United States of America

asked on

Centering popup windows in the middle of your browser window

The script below works in both Safari and IE, but while it will pop open a window in Firefox, it doesn't size it or center it in the middle of the browser window.

I need it in the center in the bowser window due to multiple screens and some other issues.

Again, it works fine in both Safari and IE 7/8 but not FireFox.

Thanks for any help you can provide!
<!--//**************************************
// Name: Center Popup Windows
// Description:Center popup windows in the middle of your browser.
// By: Lewis E. Moten III
//
//
// Inputs:None
//
// Returns:None
//
//Assumes:None
//
//Side Effects:None
//This code is copyrighted and has limited warranties.
//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.2634/lngWId.2/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************
 -->
 
function getbrowserwidth()
    {
    	if (navigator.userAgent.indexOf("MSIE") > 0)
			{
        		return(document.body.clientWidth);
        	}
        else
 
 
            {
            		return window.outerWidth;
            	}
        }
        function getbrowserheight()
 
 
            {
            	if (navigator.userAgent.indexOf("MSIE") > 0)
 
 
                	{
                		return(document.body.clientHeight);
                	} 
                else
 
 
                    {
                    		return(window.outerHeight);
                    	}
                }
                var popup = new Object()
                function CenterPopup(URL, width, height)
 
 
                    {
                    	// get center of browser window
                    	var X = getbrowserwidth() / 2
                    	var Y = getbrowserheight() / 2
                    	
                    	popup = window.open(URL, 'PopUp', 'scrollbars=yes ' + 'width=' + width + ' ' + 'height=' + height + ' ' + 'top=' + (window.screenTop + (Y - (height/2))) + ' ' + 'left=' + (window.screenLeft + (X - (width/2))) 
                    		)
                    	
                    	popup.focus()
                }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GSMadMan

ASKER

Thank you for your code repair! It works fine in all 3 browsers (Safari, Firefox and IE) that we support and test for.

While I didn't write the above code, comparing your changes have helped me a lot!

Thank you!