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!
codefingerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.