Link to home
Start Free TrialLog in
Avatar of champ_010
champ_010

asked on

onUnload Close PopUp Window!

I have a standard popup window and a frameless sliding window that appears onLoad of my webpage.
The frameless sliding window has an autoclose onUnload of the parent window, but the standard popup does not.
How do I ad this to my <body> tag of the parent window?

This is what I have so far, but not working:

<body onload="openFrameless(); popWindow('window.htm',name,'190','120','no');return false;" onUnload="popWindow.close()">

Thanks.
Avatar of netuser1976
netuser1976

You need to create a "handle" for your pop up window, so you can reference (and close it) later... try doing something like this..


<html>
<head>
<title>Untitled</title>
</head>
<script>
var pop='';
function openPop()
{
pop = window.open('window.htm','','width=190,height=120');
}

function closePop()
{
pop.close();
}
</script>
<body onload="openPop();" onUnload="closePop();">
</body>
</html>
Avatar of champ_010

ASKER


Rodnex,

I am using a script I found on the internet.  How do I integrate your example to this?:


<script type=text/javascript>

var win= null;

function popWindow(win,name,w,h,scroll){
  var winl = (screen.width-w)/2;
  var wint = 5
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
        settings +='resizable=no';

  win=window.open(win,name,settings);
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

</script>

<body onload="openFrameless(); popWindow('window.htm',name,'190','120','no');return false;">
for your <body> tag, change to this:

<body onload="openFrameless(); popWindow('window.htm',name,'190','120','no');return false;" onUnload="win.close();">
This would probably do it.  It will try to close the pop up on window unload.

 Why not include the entire page here so I can see the other functions...

<html>
<head>
<script type=text/javascript>
var win = null;
function popWindow(w)
{
win = window.open(w,'','height=120,width=190,top=5,,left=5,scrollbars=no,resizable=no');
if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function closePop()
{
if (typeof(win)!="undefined"){
win.close();
}
}
</script>
</head>
<body onload="openFrameless(); popWindow('window.htm');" onUnload="closePop();">

</body>
</html>
Your code opens the window, but it still doesn't close when I go to another page.

Here is what I have on the page with your code and the frameless window script I got from CodeLifter.com:


<html>
<head>
<title>None</title>

<script>
var win = null;
function popWindow(w)
{
win = window.open(w,'','height=120,width=190,top=5,,left=5,scrollbars=no,resizable=no');
if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function closePop()
{
if (typeof(win)!="undefined"){
win.close();
}
}
</script>


<script>
var windowW=180  
var windowH=280


var Yoffset=0   // in pixels, negative values allowed

var windowStep=2 // move increment (pixels)
var moveSpeed=10 // move speed (larger is slower)

Xmode="right";

Xoffset=18;

var urlPop = "http://www.yahoo.com"

var title =  "no title";

var autoclose = true


var windowX = (screen.width/2)-(windowW/2);
if (Xmode=="left") windowX=0+Xoffset;
if (Xmode=="right") windowX=screen.availWidth-Xoffset-windowW;
var windowY = (screen.availHeight);
var windowYstop = windowY-windowH-Yoffset;
var windowYnow = windowY;

s = "width="+windowW+",height="+windowH;
var beIE = document.all?true:false

function openAd(){
  if (beIE){
    PFW = window.open("","popFrameless","fullscreen,"+s)    
    PFW.blur()
    window.focus()      
    PFW.resizeTo(windowW,windowH)
    PFW.moveTo(windowX,windowY)
    var frameString=""+
"<html>"+
"<head>"+
"<title>"+title+"</title>"+
"</head>"+
"<frameset rows='*,0' framespacing=0 border=0 frameborder=0>"+
"<frame name='top' src='"+urlPop+"' scrolling=auto>"+
"<frame name='bottom' src='about:blank' scrolling='no'>"+
"</frameset>"+
"</html>"
    PFW.document.open();
    PFW.document.write(frameString)
    PFW.document.close()
  } else {
    PFW=window.open(urlPop,"popFrameless","scrollbars,"+s)
    PFW.blur()
    window.focus()
    PFW.resizeTo(windowW,windowH)
    PFW.moveTo(windowX,windowY)
  }  
  PFW.focus()  
  if (autoclose){
    window.onunload = function(){PFW.close()}
  }
  movePFW();
}

function movePFW(){
  if (document.all){
    if (windowYnow>=windowYstop){
      PFW.moveTo(windowX,windowYnow);
      PFW.focus();
      windowYnow=windowYnow-windowStep;
      timer=setTimeout("movePFW()",moveSpeed);
    }else{
      clearTimeout(timer);
      PFW.moveTo(windowX,windowYstop);
      PFW.focus();
    }
  }else{
    PFW.moveTo(windowX,windowYstop);
  }
}

</script>

</head>

<body onload="openAd(); popWindow('window.htm');" onUnload="closePop();">


</body>

</html>

ASKER CERTIFIED SOLUTION
Avatar of Van-Kel
Van-Kel

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
i just moved the closePop() function from <body> tag to the "window.onunload = function(){PFW.close(); closePop();}" line

oh and I forgot to close the body tag... heee...
try this,

function closePop(){
  if(win && !win.close){
    win.close();
  }
}


then do the same

<body onload="openFrameless(); popWindow('window.htm');" onUnload="closePop();">

Works great Van-Kel.  Thanks!

It is works sometimes, but  other times I am getting a javascript error:

Object expected "w" is undefined

I think it has to do with these two lines:

function popWindow(w)
{
win = window.open(w,'','height=120,width=190,top=5,,left=5,scrollbars=no,resizable=no');


What is the "w"?

Van-Kel can you respond re: your code please.
strange... "w" is just a parameter of the function popWindow, and u only call popWindow once in ur page (right?), which is in your <body onload=...> tag... and in this case, "w" will be set to "window.html".... so "w" should be defined... unless u have altered the code and called the function popWindow without any parameters..

i didn't encounter the error u are mentioning...
No worries, the error line is where the <body onload=>  tag, so it may be one of the other functions I have loading.

Thanks again.