PDA

View Full Version : Problem with window.close() after ajax-call


b_r_h
07-22-2009, 08:03 AM
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

javascript:poistu('123');

My function looks like
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?

A1ien51
07-22-2009, 12:15 PM
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

b_r_h
07-24-2009, 11:29 AM
I'm not sure, how to do callback-funktion, but I tried.


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?

A1ien51
07-24-2009, 01:19 PM
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

b_r_h
07-24-2009, 01:37 PM
Thanks Eric,

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

My onreadystatechange and readystate are there.


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);
}
}
}

A1ien51
07-24-2009, 01:49 PM
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

b_r_h
07-24-2009, 02:10 PM
If my call is async - as it should be (true last param)

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)

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