Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

unreliable javascript...what to do


The first time through this code, everything works as expected.  Invoked closely a second time,  instead of opening the new window, everything just closes...whole browser is gone, no error message.  Its LIKE it did not try to open the second window, but I cannot prove that, because when I put the alerts in, everything works!


function ButtonReturn_onclick() {

//        debugger;
    try {
        ButtonReturn.style.visibility = "hidden";
        TextURL2Grid.style.visibility = "hidden";
        TextGridHeight.style.visibility = "hidden";
        TextGridWidth.style.visibility = "hidden";
        
        window.print()

        var newwin;
        var vurl;
        var nextwin;
        var nextwidth;
        var nextheight;
    

        vurl = TextURL2Grid.value;

        window.close();

//        alert("button to call open window max");
        f_open_window_max(vurl, nextwin)

    }
    catch (err) {
        //Handle errors here
        alert(err.Description);
    }

//        open(vurl,nextwin,width=nextwidth,height=nextheight,scrollbars=1,resizable=1);
        

    }

    function f_open_window_max(aURL, aWinName) {


        try {
            //Run some code here

//            alert("opening next window...");
            var wOpen;
            var sOptions;

            sOptions = 'status=yes,menubar=yes,scrollbars=yes,resizable=yes,toolbar=yes,location=no';
            sOptions = sOptions + ',width=' + (screen.availWidth - 10).toString();
            sOptions = sOptions + ',height=' + (screen.availHeight - 135).toString();
            sOptions = sOptions + ',screenX=0,screenY=0,left=0,top=0';

            wOpen = window.open('', aWinName, sOptions);
            wOpen.location = aURL;
            wOpen.focus();
            wOpen.moveTo(0, 0);
            wOpen.resizeTo(screen.availWidth, screen.availHeight);
            return wOpen;


        }
        catch (err) {

            alert(err.Description);
            //Handle errors here
        }
      
      
        
    }

Open in new window


What is wrong with this code?  This is for an intranet web application, so I can be sure my users will be using IE.

Thanks in advance!
0
codefinger
Asked:
codefinger
  • 2
  • 2
1 Solution
 
sjklein42Commented:
Once you call window.close, you should not try to do anything else, since you just closed your (own) window.  There is a race condition and I can see why it sometimes works and sometimes not.

Do the window.close as the last thing in your procedure and see if it helps.

Also, you are not initializing nextwin with an explicit window name.  It might work better if you did.  Use "_blank" to create a new window.
0
 
codefingerAuthor Commented:
nextwin is a webform that was opened on the server, so I don't think it has an explicit window name that javascript would recognize....not sure about that, I am new to javascript.

Changing the order didn't help but changing the window.close() to
setTimeout("window.close()",1250)  seems to have done the trick.  Also, based on your remarks, I separated the code for printing and closing the window into two buttons instead of trying to
do it all with one button.

Thanks for the assist.  The points are yours of course but I was just wondering about that explicit window name thing...?
0
 
sjklein42Commented:
Sorry for the delay.  Been without power since last Saturday.  You should be fine without an explicit window name.  I think that _blank is the default.

Using the setTimeout is a reasonable and good way to solve the race condition.  It gives the primary Javascript thread a chance to complete all the operations before the window is destroyed.
0
 
codefingerAuthor Commented:
setTimeOut("window.close()", 1250) solves the race condition.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now