Categories
JavaScript Answers

How to detect when an image fails to load in JavaScript?

Sometimes, we want to detect when an image fails to load in JavaScript.

In this article, we’ll look at how to detect when an image fails to load in JavaScript.

How to detect when an image fails to load in JavaScript?

To detect when an image fails to load in JavaScript, we can listen for the img element’s error event.

For instance, we write

const imageFound = () => {
  alert("That image is found and loaded");
};

const imageNotFound = () => {
  alert("That image was not found.");
};

const img = new Image();
img.onload = imageFound;
img.onerror = imageNotFound;
img.src = "http://foo.com/bar.jpg";

to create an img element with Image.

Then we set img.onerror to the imageNotFound function.

imageNotFound runs when the img with the img.src URL fails to load.

Conclusion

To detect when an image fails to load in JavaScript, we can listen for the img element’s error event.

Categories
JavaScript Answers

How to skip one test in test file Jest and JavaScript?

Sometimes, we want to skip one test in test file Jest and JavaScript.

In this article, we’ll look at how to skip one test in test file Jest and JavaScript.

How to skip one test in test file Jest and JavaScript?

To skip one test in test file Jest and JavaScript, we can use the skip method.

For instance, we write

test("it is raining", () => {
  expect(inchesOfRain()).toBeGreaterThan(0);
});

test.skip("it is not snowing", () => {
  expect(inchesOfSnow()).toBe(0);
});

to call test.skip to skip the 2nd test.

The first test will be run.

Conclusion

To skip one test in test file Jest and JavaScript, we can use the skip method.

Categories
JavaScript Answers

How to send authorization header with Axios and JavaScript?

Sometimes, we want to send authorization header with Axios and JavaScript.

In this article, we’ll look at how to send authorization header with Axios and JavaScript.

How to send authorization header with Axios and JavaScript?

To send authorization header with Axios and JavaScript, we can set the headers property in the request options object.

For instance, we write

const response = await axios.get(url, {
  headers: {
    Authorization: `Bearer ${token}`,
  },
});

to make a get request to the url with axios.get.

We set the Authorization header by adding the Authorization property to headers.

And we get the response from the returned promise with await.

Conclusion

To send authorization header with Axios and JavaScript, we can set the headers property in the request options object.

Categories
JavaScript Answers

How to remove HTML tags in JavaScript with regex?

Sometimes, we want to remove HTML tags in JavaScript with regex.

In this article, we’ll look at how to remove HTML tags in JavaScript with regex.

How to remove HTML tags in JavaScript with regex?

To remove HTML tags in JavaScript with regex, we use a regex that matches HTML tags with content.

For instance, we write

const regex = /(<([^>]+)>)/gi;
const body = "<p>test</p>";
const result = body.replace(regex, "");

to match all tags in a case insensitive manner with /(<([^>]+)>)/gi.

The g flag finds all matches.

And the i flag matches text in a case insensitive manner.

And then we call body.replace to match all the tags and replace them with empty strings.

Categories
JavaScript Answers

How to delete all rows in an HTML table with JavaScript?

Sometimes, we want to delete all rows in an HTML table with JavaScript.

In this article, we’ll look at how to delete all rows in an HTML table with JavaScript.

How to delete all rows in an HTML table with JavaScript?

To delete all rows in an HTML table with JavaScript, we can set the table’s `innerHTML property to an empty string.

For instance, we write

const table = document.getElementById("mytable");
table.innerHTML = "";

to select the table with getElementById.

Then we set its innerHTML property to an empty string to remove all the rows.

Conclusion

To delete all rows in an HTML table with JavaScript, we can set the table’s `innerHTML property to an empty string.