Categories
JavaScript Answers

How to check if image exists on server using JavaScript?

Sometimes, we want to check if image exists on server using JavaScript.

In this article, we’ll look at how to check if image exists on server using JavaScript.

How to check if image exists on server using JavaScript?

To check if image exists on server using JavaScript, we can use the Fetch API.

For instance, we write

const res = await fetch("https://via.placeholder.com/150", { method: "HEAD" });
if (res.ok) {
  console.log("Image exists.");
} else {
  console.log("Image does not exist.");
}

to see if the image at https://via.placeholder.com/150 can be loaded by calling fetch with the URL and an object that specify that we make a HEAD request.

If res.ok if true, the image is loaded successfully.

Otherwise, the image failed to load.

Conclusion

To check if image exists on server using JavaScript, we can use the Fetch API.

Categories
JavaScript Answers

How to take a screenshot of a webpage with JavaScript?

Sometimes, we want to take a screenshot of a webpage with JavaScript.

In this article, we’ll look at how to take a screenshot of a webpage with JavaScript.

How to take a screenshot of a webpage with JavaScript?

To take a screenshot of a webpage with JavaScript, we can use Puppeteer.

To install it, we run

npm i puppeteer

Then we use it by writing

const puppeteer = require("puppeteer");

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto("https://example.com");
  await page.screenshot({ path: "example.png" });

  await browser.close();
})();

to call browser.newPage to open a new page.

Then we go to https://example.com with page.goto.

Then we take a screenshot and save it to example.png with page.screenshot.

Finally, we close the browser with browser.close.

Conclusion

To take a screenshot of a webpage with JavaScript, we can use Puppeteer.

Categories
JavaScript Answers

How to remove the first element from array and return the array minus the first element with JavaScript?

Sometimes, we want to remove the first element from array and return the array minus the first element with JavaScript.

In this article, we’ll look at how to remove the first element from array and return the array minus the first element with JavaScript.

How to remove the first element from array and return the array minus the first element with JavaScript?

To remove the first element from array and return the array minus the first element with JavaScript, we can use the array slice method.

For instance, we write

const myArray = ["item 1", "item 2", "item 3", "item 4"];
const sliced = myArray.slice(1);

to call myArray.slice with 1 to return an array with all the myArray items except the first one.

Conclusion

To remove first element from array and return the array minus the first element with JavaScript, we can use the array slice method.

Categories
JavaScript Answers

How to get date of the next day with JavaScript?

Sometimes, we want to get date of the next day with JavaScript.

In this article, we’ll look at how to get date of the next day with JavaScript.

How to get date of the next day with JavaScript?

To get date of the next day with JavaScript, we use the getDate and setDate methods.

For instance, we write

const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);

to create the tomorrow Date object with the current date and time.

Then we call getDate to get the current date.

And then we add 1 to it and use the sum as the argument for setDate to change tomorrow to tomorrow’s date.

Conclusion

To get date of the next day with JavaScript, we use the getDate and setDate methods.

Categories
JavaScript Answers

How to attach event to dynamic elements in JavaScript?

Sometimes, we want to attach event to dynamic elements in JavaScript.

In this article, we’ll look at how to attach event to dynamic elements in JavaScript.

How to attach event to dynamic elements in JavaScript?

To attach event to dynamic elements in JavaScript, we can listen for events on document.

For instance, we write

document.addEventListener("click", (e) => {
  if (e.target && e.target.id === "brnPrepend") {
    //...
  }
});

to call document.addEventListener to listen to click events on the whole document.

In the event listener callback, we check if the ID of the element that triggered the click is brnPrepend.

If it is, then we do something.

We do the element’s ID check with e.target.id === "brnPrepend".

e.target is the actual element that triggered the event.

Conclusion

To attach event to dynamic elements in JavaScript, we can listen for events on document.