Pronunciation / adjective
Bringing something good and unforseen.
    Skip Navigation Links > Code Repository
   

Open centered popup window



 
Return

Open centered popup window

This script will open a popup window centered on the user's screen.

If the dimensions of the popup are larger than the users screen then the popup will be resized to be no larger than the user's screen.

I have incorporated this script into the Javascript component which is included in the fortunate library of components, available for free and downloadable from here: <a href="http://localhost/AboutFortunateWeb/Component-Library.aspx" target="_top">Component Library</a>.

Add the following script between the head tags of your page:
  1. <script language="javascript">
  2. <!--
  3. function CenterPopup(location, window_name, toolbar, locationbar, directoriesbar, statusbar, menubar, scrollbars, resizable, iwidth, iheight)
  4. {
  5. if (document.all)
  6. {
  7. var xMax = screen.width, yMax = screen.height
  8. }
  9. else if (document.layers)
  10. {
  11. var xMax = window.outerWidth, yMax = window.outerHeight
  12. }
  13. else
  14. {
  15. var xMax = 640, yMax=480;
  16. }
  17. if (xMax < iwidth)
  18. {
  19. iwidth = xMax, iheight = yMax
  20. }
  21. var xOffset = (xMax - iwidth)/2, yOffset = (yMax - iheight - 75)/2;
  22. var options = ''
  23. if (toolbar == 'True')
  24. {
  25. var options = 'toolbar=yes'
  26. }
  27. else
  28. {
  29. options = 'toolbar=no'
  30. }
  31. if (locationbar == 'True')
  32. {
  33. options = options+', location=yes'
  34. }
  35. else
  36. {
  37. options = options+', location=no'
  38. }
  39. if (directoriesbar == 'True')
  40. {
  41. options = options+', directories=yes'
  42. }
  43. else
  44. {
  45. options = options+', directories=no'
  46. }
  47. if (statusbar == 'True')
  48. {
  49. options = options+', status=yes'
  50. }
  51. else
  52. {
  53. options = options+', status=no'
  54. }
  55. if (menubar == 'True')
  56. {
  57. options = options+', menubar=yes'
  58. }
  59. else
  60. {
  61. options = options+', menubar=no'
  62. }
  63. if (scrollbars == 'True')
  64. {
  65. options = options+', scrollbars=yes'
  66. }
  67. else
  68. {
  69. options = options+', scrollbars=no'
  70. }
  71. if (resizable == 'True')
  72. {
  73. options = options+', resizable=yes'
  74. }
  75. else
  76. {
  77. var options = options+', resizable=no'
  78. }
  79. window.open(location, window_name, options+', width='+iwidth+', height='+iheight+', screenX='+xOffset+', screenY='+yOffset+', top='+yOffset+', left='+xOffset+'');
  80. }
  81. //-->
  82. </script>


Popup the window from any object on the page by calling the CenterPopup function.
  1. <input type="button" id="openpopup" value="open" onClick="javascript:CenterPopup('http://www.aboutfortunate.com', 'AboutFortunate', 'true', 'true', 'true', 'true', 'true', 'true', 'true', 800, 600);">


Return
Microsoft Certified Professional   © 2018 Fortunate.  All rights reserved.
contact: justin@aboutfortunate.com