Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2008
    Location
    Western Belgium
    Posts
    32
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Problem with window.close() after ajax-call

    Hello again,

    I have done some ajax-functions which are working ok. Then I must include one call on closing function of form. Function is linked as

    Code:
    javascript:poistu('123');
    My function looks like
    Code:
    function poistu(lista) {
       KasitteleSession('ajax/do_list.php','listat='+lista,Tarkista,true);
       alert('end of function');
       window.close();
    }
    Problem: After calling ajax-function KasitteleSession, I should close my active window (here above window.close()).
    It is working and also ajax-call is working ok when alert has been called before.
    If alert is not there. window.close are not working and my ajax-call fails on XMLHttpRequest.Status = 0.
    Any ideas, how to handle this?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You are making an asynchronous request it does not wait around until it is finished. It fires off the code and it keeps on running the rest of the code after it.

    If you want the code to run, you should be calling the window.close in the callback function.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Dec 2008
    Location
    Western Belgium
    Posts
    32
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I'm not sure, how to do callback-funktion, but I tried.

    Code:
    function closewindow(to_do)
    {
       eval(to_do);
    }
    
    function poistu(lista) {
       KasitteleSession('ajax/do_list.php','listat='+lista,Tarkista,true);
       //alert('end of function');
       closewindow("window.close()");
    }
    when my code is as above, (ajax call is async), it's eventhandler cannot still update because of status 0. If alert is active, it is working. Window closes itself.

    Ideas?

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    The callback function is somewhere in this call KasitteleSession. You may want to read up on Ajax before you use it and learn about how it works. You need to work with onreadystatechange and readystate equal to four.

    You are basically choose to start to juggle with chainsaws when you have never juggled before in your life.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    New Coder
    Join Date
    Dec 2008
    Location
    Western Belgium
    Posts
    32
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks Eric,

    Yeps, Maybe I try to do some shortcuts - too much :-)

    My onreadystatechange and readystate are there.

    Code:
    var Pyynto; 
    
    function alustaPyynto() 
    { 
        if(window.ActiveXObject) { 
            Pyynto = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        else if(window.XMLHttpRequest) { 
            Pyynto = new XMLHttpRequest(); 
        }
        return Pyynto;	
    }
    
    function KasitteleSessiot(koodi,parametri,funktio,async_sync) 
    { 
        alustaPyynto(); 
        Pyynto.onreadystatechange = funktio; 	
        Pyynto.open("POST", koodi, async_sync); 
        Pyynto.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
        Pyynto.send(parametri); 	
    }
    
    function Tarkista() 
    { 
        var win = window.opener;
    
        if(Pyynto.readyState == 4) { 
            if(Pyynto.status == 200) { 
              window.close(); // this is not closing window.
            } else { 
                alert("Error! [Tarkista] "+Pyynto.status); 
            } 
        } 
    }

  • #6
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Are you sure if that it is going into that if statement. Add an alert. Is the browser giving you any error messages? Is the page posting back for some reason?

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #7
    New Coder
    Join Date
    Dec 2008
    Location
    Western Belgium
    Posts
    32
    Thanks
    8
    Thanked 0 Times in 0 Posts
    If my call is async - as it should be (true last param)
    Code:
    KasitteleSession('ajax/do_list.php','listat='+lista,Tarkista,true);
    I cannot reach it, because I got Pyynto.status = 0

    If I add after call alert on poistu-function it works ok, but this is not we want.

    If my call is sync - as it should be (false last param)
    Code:
    KasitteleSession('ajax/do_list.php','listat='+lista,Tarkista,false);
    I cannot reach it because function is not called and I can referer direct to responseText and can close window, but my call is sync


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •