Categories
JavaScript Answers

How to reload a page every X seconds with JavaScript?

Sometimes, we want to reload a page every X seconds with JavaScript.

In this article, we’ll look at how to reload a page every X seconds with JavaScript.

How to reload a page every X seconds with JavaScript?

To reload a page every X seconds with JavaScript, we can use the setInterval function.

For instance, we write:

setInterval(() => {
  location.reload(true);
}, 5000);

to call setInterval with a function that calls location.reload to reload the page.

The 2nd argument is the interval to wait until the callback is called again.

Conclusion

To reload a page every X seconds with JavaScript, we can use the setInterval function.

Categories
JavaScript Answers

How to get the index from a JSON object with value with JavaScript?

Sometimes, we want to get the index from a JSON object with value with JavaScript.

In this article, we’ll look at how to get the index from a JSON object with value with JavaScript.

How to get the index from a JSON object with value with JavaScript?

To get the index from a JSON object with value with JavaScript, we can use the array findIndex method.

For instance, we write:

const data = [{
  "name": "allFit",
  "section": "left"
}, {
  "name": "allbMatches",
  "section": "left"
}, {
  "name": "allOffers",
  "section": "left"
}, {
  "name": "allInterests",
  "section": "left"
}, {
  "name": "allResponses",
  "section": "left"
}, {
  "name": "divChanged",
  "section": "right"
}];

const val = "allInterests"
const index = data.findIndex((item) => {
  return item.name === val
});

console.log(index);

to call data.findIndex with a callback that finds the first item where the name property of an object in data is equal to val and returns the index of that object.

Therefore, index is 3 since the 4th object in data has name set to 'allInterests'.

Conclusion

To get the index from a JSON object with value with JavaScript, we can use the array findIndex method.

Categories
JavaScript Answers

How to retrieve file names out of a multi-file upload control with JavaScript?

Sometimes, we want to retrieve file names out of a multi-file upload control with JavaScript.

In this article, we’ll look at how to retrieve file names out of a multi-file upload control with JavaScript.

How to retrieve file names out of a multi-file upload control with JavaScript?

To retrieve file names out of a multi-file upload control with JavaScript, we can get them from the change event’s files property.

For instance, we write:

<input type='file' multiple>

to add a file input that accepts multiple files.

Next, we write:

const input = document.querySelector("input");
input.onchange = (e) => {
  for (const f of e.target.files) {
    console.log(f.name)
  }
}

to select the file input with querySelector.

Then we set the input.onchange property to add a change event handler to the file input.

In it, we loop through the selected files with e.target.files with the for-of loop.

And then we get the file name of the selected file with f.name.

Conclusion

To retrieve file names out of a multi-file upload control with JavaScript, we can get them from the change event’s files property.

Categories
JavaScript Answers

How to convert data URL to image data with JavaScript?

Sometimes, we want to convert data URL to image data with JavaScript.

In this article, we’ll look at how to convert data URL to image data with JavaScript.

How to convert data URL to image data with JavaScript?

To convert data URL to image data with JavaScript, we can create a canvas with the image.

For instance, we write:

const convertURIToImageData = (url) => {
  return new Promise((resolve, reject) => {
    if (!url) {
      return reject();
    }
    const canvas = document.createElement('canvas')
    const context = canvas.getContext('2d')
    const image = new Image();
    image.onload = () => {
      canvas.width = image.width;
      canvas.height = image.height;
      context.drawImage(image, 0, 0, canvas.width, canvas.height);
      resolve(context.getImageData(0, 0, canvas.width, canvas.height));
    }
    image.crossOrigin = "Anonymous";
    image.src = url;
  });
}
const url = "https://picsum.photos/200/300";
const load = async () => {
  const img = await convertURIToImageData(url)
  console.log(img)
}
load()

to create the convertURIToImageData function that takes a url.

In it, we return a promise that checks if the url is set.

If it’s not, we reject the promise.

Otherwise, we create a canvas with createElement.

Then we get its context with getContext.

Next, we create an img element with the Image constructor.

We set image.onload to a function to draws the image in the canvas bvy setting the canvas’ size to the image size.

Then we call drawImage to draw the image.

And then we call resolve with the image data we get from getImageData.

Next, we set image.crossOrigin to 'Anonymous' to let us get data from cross domain images.

And we src to url to load the image.

onload runs when the image is loaded.

Next, we define the load function that calls convertURIToImageData with url.

And then we get the img data from the promise with await.

Conclusion

To convert data URL to image data with JavaScript, we can create a canvas with the image.

Categories
JavaScript Answers

How to fix form onsubmit not working with JavaScript?

Sometimes, we want to fix form onsubmit not working with JavaScript.

In this article, we’ll look at how to fix form onsubmit not working with JavaScript.

How to fix form onsubmit not working with JavaScript?

To fix form onsubmit not working with JavaScript, we can set the form’s onsubmit property to a function that’s run when we click the submit button.

For instance, we write:

<form>
  <input type="file" name="file">
  <input type="submit">
</form>

to add a form.

We add the submit button for the form with <input type="submit">.

Next, we write:

const form = document.querySelector('form')
form.onsubmit = (e) => {
  e.preventDefault()
  console.log('submitted')
}

to select the form with querySelector.

And then we set form.onsubmit to a function that calls preventDefault to stop the default server side behavior and log 'submitted'.

Therefore, when we click on Submit, we see 'submitted' logged.

Conclusion

To fix form onsubmit not working with JavaScript, we can set the form’s onsubmit property to a function that’s run when we click the submit button.