codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Pop up window that centres on the screen - problems  robskinn at 16:27 on Tuesday, September 09, 2003
 

Hi, I am trying to put a jumble of code together to allow a pop-up window to centre on the screen, could anyone help me please :-)

This is the code I have, as you can see I have a pop-up window script and a centring window script and am using an onLoad function, I don't know how to combine them though? Thanks

 The full html code:  



<html>
<head>

<title>Fruehauf</title>

<script language="JavaScript">
<!--
self.moveTo(0,0)
self.resizeTo(screen.availWidth,screen.availHeight)
//-->
</script>


<script language="JavaScript">
function openCenterWin(url,theWidth,theHeight){
var theTop=(screen.height/2)-(theHeight/2);
var theLeft=(screen.width/2)-(theWidth/2);
var features=
'height='+theHeight+',width='+theWidth+',top='+theTop+',left='+theLeft+",scrollbars=no";
theWin=window.open(url,'',features);
}
</script>


<script>
var theURL = 'home.html';
var width = 754;
var height = 500;
function popWindow() {
newWindow = window.open(theURL,'newWindow','toolbar=no,menubar=no,resizable=no,scrollbars=no,status=no,location=no,width='+width+',height='+height);
}
</script>


<script language="JavaScript">
<!--
intMilSec = (10 * 1000);
function fCloseWindow() {
window.close();
}
//-->
</script>


</head>

<body bgcolor="E6E6DC" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onLoad="setTimeout('fCloseWindow()',intMilSec),popWindow()" scroll="auto" link="#EFE7DF" vlink="#EFE7DF" alink="#FF8000">
<div align="center">
<p> </p>
<p> </p>
<p> </p>
<p><a href="javascript:popWindow()"><font color="#000000">Click here</font></a>
</p>


<p><a href=javascriptpenCenterWin("home.html",754,500)> <font color="#000000">Centered
Window</font></a> </p>
</div>
</body>
</html>


  Re: Pop up window that centres on the screen - problems  Troy Wolf at 19:11 on Tuesday, September 09, 2003
 

Rather than open the window, then move it to center it, just calculate the centered position before popping the window.

Here is a tested and working code example to show you how to pop centered window. Save this code as a new HTML page then open it to see it in action.
<script language=javascript>
function WinPop(winName, winSrc, winWidth, winHeight, scroll, menubar, toolbar)
{
if (!scroll) { scroll = "no" }
var winLeft = (screen.width-winWidth)/2;
var winTop = (screen.height-winHeight)/2;
wname=window.open(winSrc, winName, "menubar="+menubar+",toolbar="+toolbar+
",resizable=yes,scrollbars="+scroll+",width="+winWidth+
",height="+winHeight+",top="+winTop+",left="+winLeft);
}
</script>
<body>
<a href=Javascript:WinPop("test","http://www.shinysolutions.com",650,400,0,0,0)>
Shiny Solutions
</a>
</body>

By the way, the window.open parameter values can be "yes"/"no" or 1/0.
Troy Wolf: site expert
SnippetEdit Website Editor


  Re: Pop up window that centres on the screen - problems  robskinn at 10:42 on Wednesday, September 10, 2003
 

Thanks for the code it combines the two separate versions well - I still need to find a way to automatically open the pop-up as the mother window opens - I have tried to use an onLoad behaviour but it does not seem to work?

Any suggestions? Many thanks.

  Re: Pop up window that centres on the screen - problems  Troy Wolf at 14:06 on Wednesday, September 10, 2003
 

Sure. For example, to pop the window from my previous example automatically when the document loads, add this code to the bottom of the script -- right before the </script> tag:
function LoadTrigger()
{
WinPop("test","http://www.shinysolutions.com",650,400,0,0,0);
}
window.onload = LoadTrigger;
Troy Wolf: site expert
SnippetEdit Website Editor


  Re: Pop up window that centres on the screen - problems  robskinn at 16:44 on Wednesday, September 10, 2003
 

Fantastic - works very well, thanks.

You may have noticed I have some script which closes the parent window after 10 seconds from being launched - is there any way which I can leave a 2 second gap before the pop-up window launches?

  Re: Javascript setTimeout to pause before popup window is opened  Troy Wolf at 17:08 on Wednesday, September 10, 2003
 

You just need to use the javascript setTimeout() function.

Copy & Paste the code below as a new HTML file, then open in browser to see it run.
<script language="javascript">
function WinPop(winName, winSrc, winWidth, winHeight, scroll, menubar, toolbar)
{
if (!scroll) { scroll = "no" }
var winLeft = (screen.width-winWidth)/2;
var winTop = (screen.height-winHeight)/2;
wname=window.open(winSrc, winName, "menubar="+menubar+",toolbar="+toolbar+
",resizable=yes,scrollbars="+scroll+",width="+winWidth+
",height="+winHeight+",top="+winTop+",left="+winLeft);
}
function Annoy()
{
WinPop("test","http://www.shinysolutions.com",650,400,0,0,0);}
function LoadTrigger()
{
// 1 second = 1000
setTimeout("Annoy()",5000);
}
window.onload = LoadTrigger;
</script>
<body>
Shiny Solutions homepage will open in a popup in 5 seconds.
</body>


If I'm helping you build a site that pops up multiple ad windows, shame on me! (Sorry, CodeToad! One or two popup ads is OK, but after navigating around CodeToad for a few minutes, I end up with 8,9,10 ad windows open! Maybe have code that only pops 2 ad windows. After that, open new ad content into the existing windows. Or only load new ad windows if the user closes the original 2.) My $0.02.
Troy Wolf: site expert
SnippetEdit Website Editor









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
• Regular Expression Query Replace -- Twice Wrongly
• Processing MS Office documents with Java
• Re: help!
• Re: Can javascript preload swf files?
• ADODB.Recordset error (0x800A0CB3)
• Need Your Inputs About Level Editor Interface
• `section` is an unexpected token. Expecting white space. Line 1, position 137.
• Javascript rollover toggle help please!
• help monitoting cpu load on windows machine


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009