0

i have been using "window.location.href" for a long time now without any issues.

the URL is calling a server process that first initiates a conversion and a download, and it usually takes just a second or two. but occasionally the server conversion/download process might take much longer. when this happens, my users see a nasty "timeout" message.

so, i am trying to determine if there is any way, possibly using the magic of jquery, something like:

$(location).attr('href',url, function()  {
     success:  {
                  // success stuff
               }
     failure:  {  // failure stuff
               }
 });

i thought about using .ajax too, but i didnt think i would be able to have the results written to the disk using jQuery. it seemed a lot easier to stick with windows.location.href if possible.

another possibility is to figure out some way to control how long "window.location.href" is willing to wait. maybe using setTimeout() somehow ?

thank you very much.

edwardsmarkf
  • 1,387
  • 2
  • 16
  • 31
  • you can change an iframe location instead of the window location. once you change the window location, you can't do anything else. you can even watch the iframe for onload and manually timeout after a given period if needed, at which point you can display a message, reload, etc... – dandavis Nov 15 '15 at 00:08
  • `i thought about using .ajax too, but i didnt think i would be able to have the results written to the disk using jQuery.` What do you mean by this? – L Ja Nov 15 '15 at 00:09
  • @LJa I think he means that the dom changes will not be saved if the page was saved – d.k Nov 15 '15 at 00:13
  • If you are saving data in the server, you should know that the server does not "know" if the request it is serving is an ajax request or a non-ajax request, it is just an http request, so, whatever you are doing, it doesn't matter if what triggers it is an ajax request or not – Rui Nov 15 '15 at 00:21
  • 1
    I'm pretty sure that JavaScript does not have a way to handle such errors with the `location.href` approach. The only way I know is to use ajax, as the answer below suggests. And if you are afraid of a situation, where it will not be possible to save the page with the dom changes, then you can use a base64 encode approach, as suggested here http://stackoverflow.com/questions/4498122/how-to-save-dynamicly-changed-byjquery-html-dom#answer-6802253 – d.k Nov 15 '15 at 00:21

1 Answers1

1

There's no way to handle the timeout if you are using window.location.href to trigger the request

This is how you could do your request as an ajax request with jquery:

$.get("url for request").done(function(htmlReturned) {
  //do whatever you like here on success, even window.location.href
}).fail(function(jqXhr, statusText, errorThrown){
   //handle failure here
});

You can also use $.post if you need to perform a post request. If you need to pass some parameters in the request you can do it this way $.get(url, {param1: "value1", param2: "value2"});

If you haven't done this before, the best resource is the ajax page from jquery documentation ($.get and $.post is just shorthand for ways of calling $.ajax).

Rui
  • 4,847
  • 3
  • 29
  • 35
  • thanks - i just tried this, and the results are going into the variable "htmlReturned" as expected -- however, when i use "windows.location.href" the results are correctly saved on the users hard drive. strangely, the results of $.get(htmlReturned) are not identical to what is saved using "windows.location.href". i may have to reconsider what i am trying to do, that being merely to come up with a better way to download files. – edwardsmarkf Nov 23 '15 at 02:56