unreliable javascript...what to do

Posted on 2011-10-28
Last Modified: 2012-05-12

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 { = "hidden"; = "hidden"; = "hidden"; = "hidden";

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

        vurl = TextURL2Grid.value;


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

    catch (err) {
        //Handle errors here

//        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 ='', aWinName, sOptions);
            wOpen.location = aURL;
            wOpen.moveTo(0, 0);
            wOpen.resizeTo(screen.availWidth, screen.availHeight);
            return wOpen;

        catch (err) {

            //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!
Question by:codefinger
    LVL 16

    Accepted Solution

    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.

    Author Comment

    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...?
    LVL 16

    Expert Comment

    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.

    Author Closing Comment

    setTimeOut("window.close()", 1250) solves the race condition.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 Specialist

    HTML5 development skills are critical to all developers. HTML5 is the foundation to almost any development process. That's why business, design studios, development shops and other organizations need HTML5 developers. Get your foot in the door as a HTML5 specialist.

    Suggested Solutions

    Title # Comments Views Activity
    network + 7 57
    Make code non clickable 6 27
    VB.NET Server side Javascript 8 43
    Sort Datagrdview programmatically 19 25
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now