Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.open() returns undefined or null on 2nd call

I have the follow scenario:

I click a link which: opens a popup window called 'popup' which loads a pdf inside of it (in IE6).

without closing the popup, i click the link again, which should reopen the pdf inside the popup, but instead a javascript error in thrown: member not found

the javascript function used to open the popup is:

function openWindow(url, name, props) {
  var windowRef = window.open(url, name, props);
  if (!windowRef.opener) {
    windowRef.opener = self;
  }
  windowRef.focus(); //error at this line, windowRef must be null
  return windowRef;
}

question: how do i get around this, without opening a new popup window every time?

like image 612
mkoryak Avatar asked Dec 23 '22 11:12

mkoryak


2 Answers

this is the hack that works that everyone on the internets is using:

function openWindow(url, name, props) {
  if(/*@cc_on!@*/false){ //do this only in IE
    var windowRef = window.open("", name, props);
    windowRef.close();
  }
  var windowRef = window.open(url, name, props);
  if (!windowRef.opener) {
    windowRef.opener = self;
  }
  windowRef.focus();
  return windowRef;
}
like image 104
mkoryak Avatar answered Dec 27 '22 03:12

mkoryak


try to use global var windowRef outside the function openWindow(). Something like this:

var WindowRef = null;

function openWindow(url, name, props) {
  if(WindowRef == null){
    WindowRef = window.open(url, name, props)
  }
  else{
    WindowRef.document.location = url
  }
  if (!WindowRef.opener) {
    WindowRef.opener = self;
  }
  WindowRef.focus();
  return WindowRef;
}
like image 45
Sergei Kovalenko Avatar answered Dec 27 '22 03:12

Sergei Kovalenko