js check if image url 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();

4
7
Glwday 75 points

                                    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");

4 (7 Votes)
0
4.43
7
Scott.korin 125 points

                                    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");

4.43 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
javascriptcheck if image exists javascript check if image source exists javascript check if img src exists javascript check url image exists js check if picture in picture exists how verify if url image exists in js javascript does image exist how to check if an image exists in js and return that url javascript check if image exists else how to check if an image exists in js check if something is an image url javascript javascript if image exist check if a url has image js check if a link for image exist js javascript is image url exists js check if image exists in directory check if image exists js javascript check if image from url exists check to see if image exists in folder javascript check if url is image javascript html check if image url exists check if image src exists javascript javascript check if image exists in folder How to check if image exists with given url node js javascript check if image file exists how to check image exist in folder in javascript javascript check if image exists quickly node check if image exists javascript check image exist on server js check if image exists on server javascript image exists check if an url is an image js js check if url is image js how to check if image exists check if url contains image js fastest way if img exists javascript check if url is image js javascript verify if is image from url javascript check if image src exists js check if image url exists check if image exists by url js javascript check image url exists check if image existes javasceipt javascript check if an image exists online javascript test if image url exists javascript check image src exists angular check if image src exists javascript validate image url javascript check if image url exists js test if image error how to check if the image file exists in js javascript check if image url exist return image only if exist js javascript check if image exisst image file exist check js check image exists or not on server using javascript check image exist or not in javascript check image exists javascript check whether image exists javascript check if image exists at url javascript check if image exists in url javascript javascript test image exists check exist img src js check if image file is found javascript check if image exists at url javascript check if the image address exist in the file how do I know when a file is not an image in js js check if image is broken js check if image exists how to check if img src exists javascript test if image exists javascript how to check if image exists check if img src exists check image exist in javascript way to check if image exists javascript validate image exist javascript exists img js check exiss img js how to check if an image is available or not javascript javascript check image exists url checking the images whether it is present in folder in js check if image source exists javascript check if remote image exist javascript check if image exist javascript javascript check if url image exists check if an image exists javascript javascript check if image exists check if image exists javascript check if image is present on a path javascript check if image url exists javascript if image exists javascript how to check if a file is an image in js react how to check if image (url) exists in javascript check if image exists in path js js if image on file path exists check image path exists javascript function to check if an image exists
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source