0

I'm creating a form tag and submit it with certain data, to download a file without reloading the entire page.

Form tag is made invisible and appended to the body element.

        var frm = $('<form />');
        frm.attr('method', 'POST');
        frm.attr('action', url);
        frm.attr('display', 'none');
        $(document.body).append(frm);
        frm.submit();

Everything works fine, except that after some time I see these invisible forms accumulating in the body element, even after their work is done, wasting memory.

How can I programmatically detect that form's work is done, and destroy the form element?

I understand that I can use a timeout with a reasonable delay to do this, but need to know if there is a better way? Perhaps by using a IFRAME?

BuddhiP
  • 6,231
  • 5
  • 36
  • 56
  • 1
    Would an ajax request not be more suited for that purpose. There you have callback functions to do exactly what you want. I think that is the way jquery and other tools are doing it and there is no such attribute to check if a form has been submitted on the server. – Koogle Feb 20 '15 at 17:14
  • `frm.attr('display', 'none');`, i guess you mean `frm.css('display', 'none');` – A. Wolff Feb 20 '15 at 17:20
  • @Koogle Ajax requests work if I want to get some data down, here I want to download a file and I want the browser to show the 'Save As' dialog. That doesn't happen if I use the normal Ajax request. That's the issue. – BuddhiP Feb 20 '15 at 17:20
  • @A.Wolff well, it works with attr as well. But I agree css is a better alternative. – BuddhiP Feb 20 '15 at 17:21
  • I don't know any attribute `display` but as your FORM is empty, there is no purpose to set its display property anyway – A. Wolff Feb 20 '15 at 17:23
  • `How can I programmatically detect that form's work is done` Bind onsubmit event (delegated or not) to the FORM. But i'm not sure what is your issue. Could you replicate it? – A. Wolff Feb 20 '15 at 17:25
  • But can't you just submit the form data via post and then use done() to show your Dialog. – Koogle Feb 20 '15 at 17:26
  • @A.Wolff, Thanks for the tips. But my issue is something else. I don't need to use onsubmit, I submit the form myself in code. I need to know when the request has completed, not when it's sent. :) – BuddhiP Feb 20 '15 at 17:27
  • Which request are you talking about? Submiting FORM should be synchronous – A. Wolff Feb 20 '15 at 17:28
  • t0 --> I create a form. t1 --> I submit it. t2 --> File download begins. t3 --> File is completly downloaded. I need to be notified at t3. :) – BuddhiP Feb 20 '15 at 17:30
  • 1
    Could help: http://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download – A. Wolff Feb 20 '15 at 17:31

0 Answers0