Answers for "js check if image url exists"

0

js check if image url exists

function checkImage(url) {
  var request = new XMLHttpRequest();
  request.open("GET", url, true);
  request.send();
  request.onload = function() {
    status = request.status;
    if (request.status == 200) //if(statusText == OK)
    {
      console.log("image exists");
    } else {
      console.log("image doesn't exist");
    }
  }
}
checkImage("https://picsum.photos/200/300");
Posted by: Guest on April-12-2021
0

javascript how to check if image exists

// create an XHR object
const xhr = new XMLHttpRequest();

// listen for `onload` event
xhr.onload = () => {
    if (xhr.status == 200) {
        console.log('Image exists.');
    } else {
        console.log('Image does not exist.');
    }
};

// create a `HEAD` request
xhr.open('HEAD', '/img/bulb.svg');

// send request
xhr.send();
Posted by: Guest on August-08-2020
0

js check if image url exists

function checkImage(url) {
  var request = new XMLHttpRequest();
  request.open("GET", url, true);
  request.send();
  request.onload = function() {
    status = request.status;
    if (request.status == 200) //if(statusText == OK)
    {
      console.log("image exists");
    } else {
      console.log("image doesn't exist");
    }
  }
}
checkImage("https://apitest.cargoxrate.com/cache/upload/39908016179913740685.jpg?https://picsum.photos/200/300");
Posted by: Guest on April-12-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language