Categories
JavaScript Answers

How to decode a string with escaped Unicode with JavaScript?

Sometimes, we want to decode a string with escaped Unicode with JavaScript.

In this article, we’ll look at how to decode a string with escaped Unicode with JavaScript.

How to decode a string with escaped Unicode with JavaScript?

To decode a string with escaped Unicode with JavaScript, we can use the decodeURIComponent function.

For instance, we write

const unescaped = decodeURIComponent(
  JSON.parse('"http\\u00253A\\u00252F\\u00252Fexample.com"')
);

to call decodeURIComponent with the string parsed with JSON.parse.

Then we the Unicode string will be unerscaped by decodeURIComponent.

Conclusion

To decode a string with escaped Unicode with JavaScript, we can use the decodeURIComponent function.

Categories
JavaScript Answers

How to get an object’s absolute position on the page in JavaScript?

Sometimes, we want to get an object’s absolute position on the page in JavaScript.

In this article, we’ll look at how to get an object’s absolute position on the page in JavaScript.

How to get an object’s absolute position on the page in JavaScript?

To get an object’s absolute position on the page in JavaScript, we can use the getBoundingClientRect method.

For instance, we write

const logo = document.getElementById("hlogo");
const logoTextRectangle = logo.getBoundingClientRect();

console.log(logoTextRectangle.left);
console.log(logoTextRectangle.right);

to select the element with getElementById.

Then we get an object with the position values with the getBoundingClientRect method.

Next, we get the left and right position values from the object returned by getBoundingClientRect.

Conclusion

To get an object’s absolute position on the page in JavaScript, we can use the getBoundingClientRect method.

Categories
JavaScript Answers

How to make TinyMCE paste in plain text by default with JavaScript?

Sometimes, we want to make TinyMCE paste in plain text by default with JavaScript.

In this article, we’ll look at how to make TinyMCE paste in plain text by default with JavaScript.

How to make TinyMCE paste in plain text by default with JavaScript?

To make TinyMCE paste in plain text by default with JavaScript, we can set the paste_as_text option to true.

For instance, we write

tinymce.init({
  plugins: "paste",
  paste_as_text: true,
});

to call tinymce.init with an object with the paste_as_text property set to true to make it paste as text by default.

Conclusion

To make TinyMCE paste in plain text by default with JavaScript, we can set the paste_as_text option to true.

Categories
JavaScript Answers

How to convert a string to byte array with JavaScript?

Sometimes, we want to convert a string to byte array with JavaScript.

In this article, we’ll look at how to convert a string to byte array with JavaScript.

How to convert a string to byte array with JavaScript?

To convert a string to byte array with JavaScript, we can use the TextEncoder constructor.

For instance, we write

const utf8Encode = new TextEncoder();
const byteArr = utf8Encode.encode("abc");

to create a TextEncoder object.

Then we call utf8Encode.encode with a string to return a byte array converted from the string.

Conclusion

To convert a string to byte array with JavaScript, we can use the TextEncoder constructor.

Categories
JavaScript Answers

How to use async/await inside a React functional component with JavaScript?

Sometimes, we want to use async/await inside a React functional component with JavaScript.

In this article, we’ll look at how to use async/await inside a React functional component with JavaScript.

How to use async/await inside a React functional component with JavaScript?

To use async/await inside a React functional component with JavaScript, we can define an async function inside the component.

For instance, we write

function Dashboard() {
  const [token, setToken] = useState("");

  const getToken = async () => {
    const headers = {
      Authorization: authProps.idToken,
    };
    const response = await axios.post(
      "https://foo.bar/get-token",
      {
        //...
      },
      { headers }
    );
    const data = await response.json();
    setToken(data.access_token);
  };

  useEffect(() => {
    if (!token) {
      getToken();
    }
  }, []);

  return <>...</>;
}

to define the getToken async function in the Dashboard component.

In it, we call axios.post to make a post request.

It returns a promise and we get the resolve value of the promise with await.

Then we call getToken in the useEffect callback if token is falsy.

Conclusion

To use async/await inside a React functional component with JavaScript, we can define an async function inside the component.