Categories
JavaScript Answers

How to detect online or offline event cross-browser with JavaScript?

Sometimes, we want to detect online or offline event cross-browser with JavaScript.

In this article, we’ll look at how to detect online or offline event cross-browser with JavaScript.

How to detect online or offline event cross-browser with JavaScript?

To detect online or offline event cross-browser with JavaScript, we can use the navigator.onLine property.

For instance, we write

if (navigator.onLine) {
  // online
} else {
  // offline
}

to check the navigator.onLine value with the if statement.

If navigator.onLine is true, then the device is online.

Otherwise, it’s offline.

Conclusion

To detect online or offline event cross-browser with JavaScript, we can use the navigator.onLine property.

Categories
JavaScript Answers

How to mock an exported constant in Jest and JavaScript?

Sometimes, we want to mock an exported constant in Jest and JavaScript.

In this article, we’ll look at how to mock an exported constant in Jest and JavaScript.

How to mock an exported constant in Jest and JavaScript?

To mock an exported constant in Jest and JavaScript, we can mock the imported modules with jest.mock.

For instance, we write

const mockTrue = { ENABLED: true };
const mockFalse = { ENABLED: false };

describe("allowThrough", () => {
  beforeEach(() => {
    jest.resetModules();
  });

  test("success", () => {
    jest.mock("./constants", () => mockTrue);
    const { ENABLED } = require("./constants");
    const { allowThrough } = require("./allowThrough");

    expect(ENABLED).toBe(true);
    expect(allowThrough({ value: 1 })).toBe(true);
  });

  test("fail, ENABLED === false", () => {
    jest.mock("./constants", () => mockFalse);
    const { ENABLED } = require("./constants");
    const { allowThrough } = require("./allowThrough");

    expect(ENABLED).toBe(false);
    expect(allowThrough({ value: 1 })).toBe(false);
  });
});

to call jest.mock in each test with the module path and a function that returns the content of the module.

Then we call expect to check for the result we’re looking for in each test.

In the beforeEach callback, we call jest.resetModules to reset the modules to their original values.

Conclusion

To mock an exported constant in Jest and JavaScript, we can mock the imported modules with jest.mock.

Categories
JavaScript Answers

How to detect backspace and del on input event with JavaScript?

Sometimes, we want to detect backspace and del on input event with JavaScript.

In this article, we’ll look at how to detect backspace and del on input event with JavaScript.

How to detect backspace and del on input event with JavaScript?

To detect backspace and del on input event with JavaScript, we can check the keydown event object’s key property.

For instance, we write

input.addEventListener("keydown", ({ key }) => {
  if (["Backspace", "Delete"].includes(key)) {
    return false;
  }
});

to add a keydown event listener to the input with input.addEventListener.

In the event listener callback, we check if the key property is 'Backspace' or 'Delete'.

If it is, then we return false to stop the default action.

Conclusion

To detect backspace and del on input event with JavaScript, we can check the keydown event object’s key property.

Categories
JavaScript Answers

How to get Base64 encode file data from input form with JavaScript?

Sometimes, we want to get Base64 encode file data from input form with JavaScript.

In this article, we’ll look at how to get Base64 encode file data from input form with JavaScript.

How to get Base64 encode file data from input form with JavaScript?

To get Base64 encode file data from input form with JavaScript, we can call the FileReader object’s readAsBinaryString method.

For instance, we write

input.onchange = (event) => {
  const file = event.srcElement.files[0];
  const reader = new FileReader();

  reader.onload = () => {
    console.log(btoa(reader.result));
  };
  reader.onerror = () => {
    console.log("error");
  };

  reader.readAsBinaryString(file);
};

to set the input.onchange property to a function that runs when we change the selected file in the file input.

input is the file input.

In it, we get the first selected file with event.srcElement.files[0].

Then we create the reader FileReader object.

We then call reader.readAsBinaryString with the file to read it into a binary string/

We get the binary string in the reader.result property in the reader.onload method.

And we call btoa with the binary string to convert it to a base64 string.

Conclusion

To get Base64 encode file data from input form with JavaScript, we can call the FileReader object’s readAsBinaryString method.

Categories
JavaScript Answers

How to disable href if onclick is executed with JavaScript?

Sometimes, we want to disable href if onclick is executed with JavaScript.

In this article, we’ll look at how to disable href if onclick is executed with JavaScript.

How to disable href if onclick is executed with JavaScript?

To disable href if onclick is executed with JavaScript, we call preventDefault to stop the default navigation action.

For instance, we write

document.getElementsById("ignore-click").addEventListener("click", (event) => {
  event.preventDefault();
  //...
});

to select the link with getElementById.

Then we call addEventListener on it to add a click listener.

In it, we call event.preventDefault to stop the default navigation action.

Then we can run our onclick code after that.

Conclusion

To disable href if onclick is executed with JavaScript, we call preventDefault to stop the default navigation action.