Categories
JavaScript Answers

How to get notified about changes of the history via history.pushState with JavaScript?

Sometimes, we want to get notified about changes of the history via history.pushState with JavaScript.

In this article, we’ll look at how to get notified about changes of the history via history.pushState with JavaScript.

How to get notified about changes of the history via history.pushState with JavaScript?

To get notified about changes of the history via history.pushState with JavaScript, we can listen for changes in webNavigation.onHistoryStateUpdated.

To do this, we write

browser.webNavigation.onHistoryStateUpdated.addListener(listener, filter);

to call browser.webNavigation.onHistoryStateUpdated.addListener with the listener to listen for history.pushState calls with the listenr function.

Conclusion

To get notified about changes of the history via history.pushState with JavaScript, we can listen for changes in webNavigation.onHistoryStateUpdated.

Categories
JavaScript Answers

How to escape a JSON string containing newline characters using JavaScript?

Sometimes, we want to escape a JSON string containing newline characters using JavaScript.

In this article, we’ll look at how to escape a JSON string containing newline characters using JavaScript.

How to escape a JSON string containing newline characters using JavaScript?

To escape a JSON string containing newline characters using JavaScript, we can call string replace to replace various characters.

For instance, we write

const escape = (str) => {
  return str
    .replace(/[\\]/g, "\\\\")
    .replace(/[\"]/g, '\\"')
    .replace(/[\/]/g, "\\/")
    .replace(/[\b]/g, "\\b")
    .replace(/[\f]/g, "\\f")
    .replace(/[\n]/g, "\\n")
    .replace(/[\r]/g, "\\r")
    .replace(/[\t]/g, "\\t");
};

to replace the unescaped characters in the first argument with the escaped characters in the 2nd.

Conclusion

To escape a JSON string containing newline characters using JavaScript, we can call string replace to replace various characters.

Categories
JavaScript Answers

How to check if element is in array with JavaScript?

Sometimes, we want to check if element is in array with JavaScript.

In this article, we’ll look at how to check if element is in array with JavaScript.

How to check if element is in array with JavaScript?

To check if element is in array with JavaScript, we can use the array includes method.

For instance, we write

if (arr.includes("abc")) {
  // ...
}

to call arr.includes with 'abc' to check if the arr array has 'abc' in it.

Conclusion

To check if element is in array with JavaScript, we can use the array includes method.

Categories
JavaScript Answers

How to fix getMonth in JavaScript giving previous month?

Sometimes, we want to fix getMonth in JavaScript giving previous month.

In this article, we’ll look at how to fix getMonth in JavaScript giving previous month.

How to fix getMonth in JavaScript giving previous month?

To fix getMonth in JavaScript giving previous month, we add 1 to the month returned by getMonth.

For instance, we write

const m = d.getMonth() + 1;

to add 1 to the month returned by d.getMonth to get the human readable month number from date d.

This is because getMonth returns 0 for January, 1 for February, etc.

Conclusion

To fix getMonth in JavaScript giving previous month, we add 1 to the month returned by getMonth.

Categories
JavaScript Answers

How to remove and clear all localStorage data with JavaScript?

Sometimes, we want to remove and clear all localStorage data with JavaScript.

In this article, we’ll look at how to remove and clear all localStorage data with JavaScript.

How to remove and clear all localStorage data with JavaScript?

To remove and clear all localStorage data with JavaScript, we call the localStorage.clear method.

For instance, we write

localStorage.clear();

to clear all entries in local storage.

Conclusion

To remove and clear all localStorage data with JavaScript, we call the localStorage.clear method.