Link to home
Start Free TrialLog in
Avatar of MikebEdwards
MikebEdwards

asked on

Only open a window if it doesnt already exist:

I have a page that on the body onload calls a funtcion to open a new window. The problem is that I only want this to happen when the window doesnt already exist. Hence the if !countentWindow. This doesnt work because the script doesnt no what contentWindow is. If i use a var contentWindow, then the if statement is always not true, and the eval is always called.

Any ideas?

function createContentWindow() {
      
      winHeight = screen.availHeight;
      maxX = screen.availWidth;
      mainWidth = maxX - 260;
            
      if (!contentWindow){
      eval ("contentWindow = window.open('about:blank', 'contentWindow', 'height=" + winHeight + ", width=" + mainWidth + ", toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes');");
            
      
      contentWindow.moveTo(260,0);

      contentWindow.resizeTo(mainWidth, winHeight)
      contentWindow.location.href="userview.asp";
      }

}



Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Check this:


function createContentWindow() {
   
    winHeight = screen.availHeight;
    maxX = screen.availWidth;
    mainWidth = maxX - 260;
         
    contentWindow = window.open('', 'contentWindow', 'height='+winHeight+',width='+mainWidth+',toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes');
    if(!contentWindow.document.body){
      contentWindow.location.href="userview.htm";
      contentWindow.resizeTo(mainWidth, winHeight)
      contentWindow.moveTo(260,0);
    }

}


Avatar of MikebEdwards
MikebEdwards

ASKER

That doesn't work because the    

contentWindow.location.href="userview.htm";

Is never reached.
This does seem to work though:

function createContentWindow() {
   
    winHeight = screen.availHeight;
    maxX = screen.availWidth;
    mainWidth = maxX - 260;
         
    contentWindow = window.open('', 'contentWindow', 'height='+winHeight+',width='+mainWidth+',toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes');
    if(contentWindow.location.href!='about:blank'){}
      else {
      contentWindow.location.href="userview.asp";
      contentWindow.resizeTo(mainWidth, winHeight)
      contentWindow.moveTo(260,0);
    }

}
Does this work for you:

function createContentWindow() {
   
    winHeight = screen.availHeight;
    maxX = screen.availWidth;
    mainWidth = maxX - 260;
         
    contentWindow = window.open('', 'contentWindow', 'height='+winHeight+',width='+mainWidth+',toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes');
    if(!contentWindow.myWindow){
      contentWindow.location.href="userview.htm";
      contentWindow.resizeTo(mainWidth, winHeight)
      contentWindow.moveTo(260,0);
      contentWindow.myWindow=true;
    }
}

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

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
Why did I get a grading B from you???