0

After using ajaxForm

    $('#JQF').ajaxForm({
      dataType:  'html',
      iframe: false,
      beforeSend: UP.start,
      success: UP.stop
    });

to download an entire new HTML document string (DOCTYPE, script tags and all) into a string,

I call:

reloadMain:
  function (html) {
    var newDoc = document.open("text/html", "replace");
    newDoc.write(html);
    newDoc.close();
  }
};

from UP.stop to load it into the current page.

On IE, the F12 debugger shows undefined variables popping up.

Is that from write(html) not evaluating the scripts in the same way as when loading the page online?

Is there a more correct way to do this?

Don
  • 4,245
  • 24
  • 33
  • Which variables does it say are undefined? – Barmar Sep 26 '13 at 20:38
  • Is the purpose of this to replace the current page with the html returned by the form post? – Kevin B Sep 26 '13 at 20:41
  • Yes, exactly, the page has been active running a background script while the form was submitting, now it is finished and the resulting text wants to replace the current page. – Don Sep 26 '13 at 20:43
  • that is the default action that happens when you submit a form. why are you replicating it with javascript? you aren't saving anything by doing it this way as far as resources, – Kevin B Sep 26 '13 at 20:44
  • When you replace the current document's HTML, it also replaces all the ` – Barmar Sep 26 '13 at 20:46
  • Oh no! document.write! RUN!! see: http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – nand Sep 26 '13 at 20:48
  • @Kevin, The page is running a background script communicating with the host on another task, it is also updating the page until the form in question finishes. This updating is impossible if the form is merely submitted in the normal way. – Don Sep 26 '13 at 20:52
  • @Barmar, yes, the string has ALL the – Don Sep 26 '13 at 20:53
  • @nand, Not, and I did read the referenced page. – Don Sep 26 '13 at 20:54
  • Can I put the background activity into an iframe on the page, so that it will run independently while the form under discussion submits and replaces the page in the normal way? – Don Sep 26 '13 at 20:55
  • No, because the iframe is part of the page that would be replaced. Unless you mean, would it continue to update while the form is being processed before the server returns a response, though i don't think you even need an iframe for that. – Kevin B Sep 26 '13 at 20:56
  • That was my original observation, that once the form submitted, the script I had running under a setTimeout never updated the page. So I turned the form submission into an ajaxForm call, but then I get the new page in a string, when I just want the browser to load it as the new page. – Don Sep 26 '13 at 21:00
  • 1
    Based on my own testing, javascript will continue to run on a page while waiting for a response from a normal form submit. Based on that, an iframe should also continue to update and be capable of updating the parent page until the form is done submitting. – Kevin B Sep 26 '13 at 21:11
  • @Kevin, thanks, yes. I don't know what I did wrong way back when, but I have it working now, just as you indicate that it should. – Don Sep 26 '13 at 22:15

1 Answers1

0

There is no need to run the main form as an ajaxForm. The page will remain active for updates under running scripts until the main page reload from the submit.

So the final code is

    $('#JQF').on('submit', function(){
      UP.start();
      return true;
    });

where UP.start gets things rolling with a setTimeout(UP.requestInfo, 2);

Don
  • 4,245
  • 24
  • 33