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.

Categories
JavaScript Answers

How to extract base URL from a string in JavaScript?

Sometimes, we want to extract base URL from a string in JavaScript.

In this article, we’ll look at how to extract base URL from a string in JavaScript.

How to extract base URL from a string in JavaScript?

To extract base URL from a string in JavaScript, we can use the URL constructor.

For instance, we write

const url = new URL("https://example.com/foo/bar");
console.log(url.origin);

to create a URL with the URL string that we want to get the base URL from.

Then we get the base URL with the url.origin property.

Conclusion

To extract base URL from a string in JavaScript, we can use the URL constructor.

Categories
JavaScript Answers

How to mock the JavaScript window object using Jest?

Sometimes, we want to mock the JavaScript window object using Jest.

In this article, we’ll look at how to mock the JavaScript window object using Jest.

How to mock the JavaScript window object using Jest?

To mock the JavaScript window object using Jest, we can call the jest.spyOn method.

For instance, we write

let windowSpy;

beforeEach(() => {
  windowSpy = jest.spyOn(window, "window", "get");
});

afterEach(() => {
  windowSpy.mockRestore();
});

it("should return https://example.com", () => {
  windowSpy.mockImplementation(() => ({
    location: {
      origin: "https://example.com",
    },
  }));

  expect(window.location.origin).toEqual("https://example.com");
});

it("should be undefined.", () => {
  windowSpy.mockImplementation(() => undefined);

  expect(window).toBeUndefined();
});

to create the windowSpy with jest.spyOn.

And then we call windowSpy.mockImplementation with a function that returns a mocked window object.

Then we call expect to check the value of the mocked window.location.origin property in the test.

Likewise, we call mockImplementation with a callback that returned undefined in the 2nd test.

Then window would be undefined in the 2nd test.

We call windowSpy.mockRestore after each test to restore it to the original value.

Conclusion

To mock the JavaScript window object using Jest, we can call the jest.spyOn method.