Solved

Centering popup windows in the middle of your browser window

Posted on 2009-07-08
2
481 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:GSMadMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 24811792
VERY poor code with MANY issues and syntax errors

Please see this

<script><!--
//**************************************
// Name: Center Popup Windows
// Description:Center popup windows in the middle of your browser.
// By: Lewis E. Moten III
// Seriously Fixed: Michel Plungjan, javascripts(at)plungjan.name
//
// Inputs:None
//
// Returns:false if successfull open of window, true if not
//
//Assumes:None
//
//Side Effects:None
//This code contains so little of the original code that it can no longer be said to be copyrighted by the original author and has limited warranties.
//Please do not try to find the code here anymore: http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.2634/lngWId.2/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************
//
 
function getbrowserwidth() {
  return (window.outerWidth)? window.outerWidth: document.body.clientWidth;
}
function getbrowserheight() {
return (window.outerHeight)?window.outerHeight:document.body.clientHeight;
}
function CenterPopup(URL, target, width, height) {
  // get center of browser window
  var X    = getbrowserwidth() / 2
  var Y    = getbrowserheight() / 2
  var winTop  = (window.screenTop  !=null)?window.screenTop:window.screenY;
  var winLeft = (window.screenLeft !=null)?window.screenLeft:window.screenX;
  var top  = (winTop+ (Y - (height/2)))
  var left = (winLeft + (X - (width/2)))
  var popup = window.open(URL, target, 'scrollbars=yes,width='+width+',height='+height+',top='+top+',left='+left);
  if (popup) {
    popup.focus()
    return false;
  }
  return true; // follow link, user has a popup blocker
}
// --></script>
<a href="http://www.google.com/search?q=center+popup" onClick="return CenterPopup(this.href,this.target,400,500)" target="PopUp">Click</a>

Open in new window

0
 

Author Closing Comment

by:GSMadMan
ID: 31601384
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!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question