简体   繁体   中英

Javascript: closing a popup window

I am a JS noob, so please excuse me if this question is too "noobish".

Note: the function is long, but just scroll down till you see // * ** * ** * ** as that's the important part.

My function:

function ShowWindowN(userId,url,width,height,courseId) {
  var iev=getIEVer();
  if(iev>=6&&iev<8){
    width+=16*1;
  }
  var w=screen.availWidth<width?screen.availWidth:width;
  var csCookie=readCookie('course_settings_'+userId);
  var found=false;
  if(csCookie!=null){
    csCookie=eval('({'+csCookie.replace(/&/g,',').replace(/=/g,':')+'})');
    if(csCookie[courseId]!=null){
      height+=1*csCookie[courseId].height;
      found=true;
    }
  }
  if(!found){
    var plCookie=readCookie('player_settings_'+userId);
    if(plCookie!=null){
      plCookie=eval('({'+plCookie.replace(/&/g,',').replace(/=/g,':')+'})');
      height+=1*plCookie.window_extra_height;
    }else{
      height+=16;
    }
  }
  var h=screen.availHeight<height?screen.availHeight:height;
  var left = (screen.availWidth/2)-(w/2);
  //************
  objMovieWindow=window.open(url, "movieWindow", "width=" + w + ", height=" + h + ", top=0, left="+left+", status=false, toolbar=false, menubar=false, location=false, directories=false, scrollbars=1, resizable=1"); 
  objMovieWindow.focus();
  //alert(objMovieWindow.name);
};

If you look at the alert() on the 2nd last line, that does not work, it gives me a "permissions denied" error. I also have a function that says "close_me()" with just this:

objMovieWindow.close();

But that does not work and gives me an "undefined" error. Basically, I load the page, and click a link to launch the popup, then the parent page reloads, and by clicking a link on the parent page I want to close the popup.

Please tell me how I can close the damn popup window?

When you reload the page you are losing the reference to objMovieWindow , so you won't be able to call objMovieWindow.close() . You'll have to avoid reloading the page if you want to close the window.

UPDATE

One work around would be to call window.open() again with the same name, and call close() on the reference returned from that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM