views:

76

answers:

1

Fetch a URL - mywebiste.com/blah.php --> returns 404

Fetch a URL - mywebsite.com/blah.php --> returns 404

Fetch a URL - mywebsite.com/blah.php --> returns 404

Fetch a URL - mywebsite.com/blah.php --> returns 200 // how do I loop until this the URL returns 200?

Previously phrased:

So I have a URL (foo) that I need to download after my page has finished rendering or the element that I want to show it in is on the page i.e. I'll call this function that I'm asking about from my element.

The problem is I have to keep downloading the URL until it returns 200, it starts of saying 404, and gives 200 and a bunch of other headers, once it's done.

Can anyone think of a quick and easy way of doing this? I'm sort of brain-dead @ 2:50AM :(

A: 

Try something like this - load the page, and try again and again on error. Make sure to add the rest of the arguments to ajax. You can add a check to load only on error code 404:

function tryLoad
    $.ajax({
        url: 'url',
        success: function(data){ /* handle load */ },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            setTimeout(tryLoad, 1000); //try again later
        }
    });
}

Of course, this will load indefinitely if you have a real error.

Kobi
trying this out, looks promising will mark as answer if it works!
halivingston