tags:

views:

31

answers:

4

I am executing a cgi file directly from javascript onclick of a button. The cgi return the doc, xls, exe etc file which in turn opens SavaAs dialog to let the client save file on his machine. Problem comes when multiple cgi files are executing in a for/while loop, It executes only the first cgi and opens the SaveAs dialog, but once the SaveAs opens it does not enter into the "for" loop again to execute another cgi which opens SaveAs dialog .

Here is the code fragment -

for(i = 0; i < dataPopup.elements['checkbox'].length; i++)      
{
    j=0;
    obj = dataPopup.elements['checkbox'];
    if(obj[i].checked)
    {
        var temp=obj[i].value;
        temp = temp.split(".");
        if(temp[1] == "txt")
        {
            saveWins[temp[1]] = setTimeout("document.location='../cgi/saveTextFile.cgi?fname=070319185708701_1'", 100);
        }
        else if(temp[1] == "pdf")
        {
            saveWins[temp[1]] = setTimeout("document.location='../cgi/savePdfFile.cgi?fname=065726729272220_1'", 100);
        }
        else if(temp[1] == "xls")
        {
            saveWins[temp[1]] = setTimeout("document.location = '../cgi/saveXlsFile.cgi?fname=288433243743'", 100);
        }
        else if((temp[1] == "doc") || (temp[1] == "docx"))
        {
            saveWins[temp[1]] = document.location = '../cgi/saveDocFile.cgi?fname='+temp[0];
        }
        saveWins[temp[1]].focus();
    }
}

Please Help.

+1  A: 

Setting document.location replaces the current document with the new document - even if it's a download. That means there is no longer a script to continue to execute.

You'll need to set the location of an iframe instead.

RoToRa
Thanks for the reply.I got the reason as why it is not working, but what do you mean by "You'll need to set the location of an iframe instead." Which iframe are you taking about.
Neha
A: 

Thanks for the reply. I got the reason as why it is not working, but what do you mean by "You'll need to set the location of an iframe instead." Which iframe are you taking about?

Neha
A: 

Hi Neha,

RoToRa means that instead of using document.location you should insert a hidden iFrame in your page and change the location of the iframe using window.frames[iframeName].location.

HTH,
Miguel

Michi
A: 

Hey,,

Thanks a lot. It worked. However one dialog gets escaped by other if they are opened at same time, I have used setTimeout function and given the time gap of atleast 5 seconds then only every dialog opens. Like in first loop 1000 milliseconds, in 2nd 6000 milliseconds, in 3rd 11000 milliseconds and so on. This leaves to a slower execution.

Do you have any better method for this ?

Neha