-1

I have found many questions on this topic including stackoverflow. But none of them working for me. I have compiled the following from the existing answers:

function doesFileExist(urlToFile)
{
   var xhr = new XMLHttpRequest();
   xhr.open('HEAD', urlToFile, false); // => Error message on this line
   xhr.send();

   if (xhr.status == "404") {
     return false;
   }else {
     return true;
   }
}

Then I use this function to call my web server as follows:

var theSource = "myurl";
var result = doesFileExist(theSource);
if (result == true) {
    // yes, file exists!
    console.log('file exists:', theSource);
} else {
    // file does not exist!
    console.log('file does not exist:', theSource);
}

The error message is this:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

MGZ
  • 141
  • 9
  • I have added a link to another tutorial that might help you get your head around what is going on here. – Sudsy Jun 11 '16 at 22:22
  • That isn't an error message … and it won't stop your code from working. – Quentin Jun 11 '16 at 22:24
  • @Quentin is right but the message is there for a reason. If you need to check of files it may lock up your user interface. – Sudsy Jun 12 '16 at 02:36

1 Answers1

-2

The documentation here should be enough to get you started on asynchronous requests.

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests

If that explanation is not sufficient, try the following tutorial about writing simple async functions.

http://cwbuecheler.com/web/tutorials/2013/javascript-callbacks/

Here's another example that might help.

How can I take advantage of callback functions for asynchronous XMLHttpRequest?

You could use a variation of the function presented in the last link, something like this. (Edited since original posting)

function getRequestStatus(url, callback) 
    {
        var request = new XMLHttpRequest();
        request.onreadystatechange = function()
        {
            if (request.readyState == 4)
            {
                callback(request.status); 
            }

        }; 
        request.open('GET', url);
        request.send();
    }

 getRequestStatus("http://httpbin.org/status/404", function(status){
     if(status == 404){
           //It's not found so do something here
          console.log("Not Found")
        }
    });

But you will probably need to read the articles to understand how to use this function.

Once you have read the articles and run through the tutorial you may understand the syntax below a little better. If you don't take the time to understand what is going on here, you will be struggling with the very next steps like how to use callbacks in loops etc.

Community
  • 1
  • 1
Sudsy
  • 931
  • 7
  • 16
  • Please [avoid link only answers](http://meta.stackoverflow.com/tags/link-only-answers/info). Answers that are "barely more than a link to an external site” [may be deleted](http://stackoverflow.com/help/deleted-answers). – Quentin Jun 11 '16 at 22:24
  • Still not clear how to use the callback. How can I access the result of this callback which is false or true? – MGZ Jun 12 '16 at 10:40
  • In progress may be but not yet concluded. – MGZ Jun 15 '16 at 10:51
  • I have made the code example runnable. You can click the Run Code Snippet button to see it working. If you are still struggling, you really should complete the following tutorial from beginning to end. http://cwbuecheler.com/web/tutorials/2013/javascript-callbacks/ – Sudsy Jun 17 '16 at 03:11