Categories
JavaScript Answers

How to catch browser’s zoom event in JavaScript?

Sometimes, we want to catch browser’s zoom event in JavaScript.

In this article, we’ll look at how to catch browser’s zoom event in JavaScript.

How to catch browser’s zoom event in JavaScript?

To catch browser’s zoom event in JavaScript, we watch the resize event.

For instance, we write

const getSizes = () => {
  const body = document.body;
  body.width = window.innerWidth;
  body.height = window.innerHeight;
  console.log(body.width, body.height);
};

getSizes();

window.addEventListener("resize", getSizes, false);

to call addEventListener to listen for the resize event.

getSizes runs when we zoom in and out since the width and height in pixels will change.

Then we get the width and height of the window with innerWidth and innerHeight.

Conclusion

To catch browser’s zoom event in JavaScript, we watch the resize event.

Categories
JavaScript Answers

How to save an image to localStorage and display it on the next page with JavaScript?

Sometimes, we want to save an image to localStorage and display it on the next page with JavaScript.

In this article, we’ll look at how to save an image to localStorage and display it on the next page with JavaScript.

How to save an image to localStorage and display it on the next page with JavaScript?

To save an image to localStorage and display it on the next page with JavaScript, we can create a canvas, put the image in it, and then convert the canvas data to a base64 string.

For instance, we write

const getBase64Image = (img) => {
  const canvas = document.createElement("canvas");
  canvas.width = img.width;
  canvas.height = img.height;
  const ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0);
  const dataURL = canvas.toDataURL("image/png");
  return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
};

const bannerImage = document.getElementById("bannerImg");
const imgData = getBase64Image(bannerImage);
localStorage.setItem("imgData", imgData);

to define the getBase64Image function that creates the canvas element with createElement.

Then we set the width and height of the canvas to the image width and height.

We draw the image on the canvas with drawImage.

And then we call toDataURL to return the base64 string with the canvas data.

Then we use it to encode the image to a string and save it to local storage with

const bannerImage = document.getElementById("bannerImg");
const imgData = getBase64Image(bannerImage);
localStorage.setItem("imgData", imgData);

We get the img element with getElementById and then call getBase64Image to get the data.

And then we call setItem to save the imgData to local storage.

Then on the destination page, we write

const dataImage = localStorage.getItem("imgData");
const bannerImg = document.getElementById("tableBanner");
bannerImg.src = "data:image/png;base64," + dataImage;

to get the local storage entry with getItem.

And we select the image with getElementById.

And then we set the src attribute of the img element with

bannerImg.src = "data:image/png;base64," + dataImage;

Conclusion

To save an image to localStorage and display it on the next page with JavaScript, we can create a canvas, put the image in it, and then convert the canvas data to a base64 string.

Categories
JavaScript Answers

How to update window.location.hash without jumping the document with JavaScript?

Sometimes, we want to update window.location.hash without jumping the document with JavaScript.

In this article, we’ll look at how to update window.location.hash without jumping the document with JavaScript.

How to update window.location.hash without jumping the document with JavaScript?

To update window.location.hash without jumping the document with JavaScript, we can use the history.pushState method or set the value of location.hash.

For instance, we write

if (history.pushState) {
  history.pushState(null, null, "#hash");
} else {
  location.hash = "#hash";
}

to call history.pushState with the hash string as the 3rd argument when it’s available.

Otherwise, we set location.hash to the hash string instead.

Conclusion

To update window.location.hash without jumping the document with JavaScript, we can use the history.pushState method or set the value of location.hash.

Categories
JavaScript Answers

How to use querySelector to find element by inner text with JavaScript?

Sometimes, we want to use querySelector to find element by inner text with JavaScript.

In this article, we’ll look at how to use querySelector to find element by inner text with JavaScript.

How to use querySelector to find element by inner text with JavaScript?

To use querySelector to find element by inner text with JavaScript, we can use the document.evaluate method.

For instance, we write

const headings = document.evaluate(
  "//h1[contains(., 'Hello')]",
  document,
  null,
  XPathResult.ANY_TYPE,
  null
);
const thisHeading = headings.iterateNext();

to call document.evaluate with the xpath that has the inner text we’re looking for in an element.

document is set as the 2nd argument so we look for the element in the whole document.

Then we get the first element that has the given xpath with iterateNext.

Conclusion

To use querySelector to find element by inner text with JavaScript, we can use the document.evaluate method.

Categories
JavaScript Answers

How to detect scroll direction with JavaScript?

Sometimes, we want to detect scroll direction with JavaScript.

In this article, we’ll look at how to detect scroll direction with JavaScript.

How to detect scroll direction with JavaScript?

To detect scroll direction with JavaScript, we can listen to the scroll event.

For instance, we write

let lastScrollTop = 0;

element.addEventListener(
  "scroll",
  () => {
    const st = window.pageYOffset || document.documentElement.scrollTop;
    if (st > lastScrollTop) {
      console.log("scrolling down");
    } else {
      console.log("scrolling up");
    }
    lastScrollTop = st <= 0 ? 0 : st;
  },
  false
);

to call addEventListener to listen to the 'scroll' event on window.

And then we get the current scroll position with

window.pageYOffset || document.documentElement.scrollTop

We then compare st with lastScrollTop to get the scroll position.

If st is bigger than lastScrollTop then we’re scrolling down.

After the if statement, we set lastScrollTop to the current scroll position which will change when we scroll again.

Conclusion

To detect scroll direction with JavaScript, we can listen to the scroll event.