Categories
JavaScript Answers

How to prompt user before browser close with JavaScript?

Sometimes, we want to prompt user before browser close with JavaScript.

In this article, we’ll look at how to prompt user before browser close with JavaScript.

How to prompt user before browser close with JavaScript?

To prompt user before browser close with JavaScript, we can return true in the beforeunload event handler.

For instance, we write:

window.onbeforeunload = (evt) => {
  return true;
}

to set the window.onbeforeunload property to a function that returns true to listen to the beforeunload event.

The beforeunload event is triggered right before we unload the page.

And since it returns true, we see a prompt before we close the browser tab.

Conclusion

To prompt user before browser close with JavaScript, we can return true in the beforeunload event handler.

Categories
JavaScript Answers

How to convert HSB/HSV color to HSL with JavaScript?

Sometimes, we want to convert HSB/HSV color to HSL with JavaScript.

In this article, we’ll look at how to convert HSB/HSV color to HSL with JavaScript.

How to convert HSB/HSV color to HSL with JavaScript?

To convert HSB/HSV color to HSL with JavaScript, we can use the formula in this wiki page.

For instance, we write:

const hsl2hsv = (h, s, l, v = s * Math.min(l, 1 - l) + l) => [h, v ? 2 - 2 * l / v : 0, v];

const hsv2hsl = (h, s, v, l = v - v * s / 2, m = Math.min(l, 1 - l)) => [h, m ? (v - l) / m : 0, l];

console.log(hsl2hsv(1, 2, 3, 4))
console.log(hsv2hsl(1, 2, 3, 4, 5))

to define the hsl2hsv and hsv2hsl functions to do the calculations.

In each function, we return the calculated values in an array.

Therefore, the first console log logs [1, 0.5, 4].

And the first console log logs [1, -0.2, 4].

Conclusion

To convert HSB/HSV color to HSL with JavaScript, we can use the formula in this wiki page.

Categories
JavaScript Answers

How to convert characters to hex in JavaScript?

Sometimes, we want to convert characters to hex in JavaScript.

In this article, we’ll look at how to convert characters to hex in JavaScript.

How to convert characters to hex in JavaScript?

To convert characters to hex in JavaScript, we can use the string charCodeAt method.

For instance, we write:

const strAsUnicode = (str) => {
  return str.split("").map((s) => {
    return `\\u${s.charCodeAt(0).toString(16).padStart(4, '0')}`;
  }).join("");
}

console.log(strAsUnicode('abc'))

to define the strAsUncode function that calls str.split to split the string into an array of characters.

Then we call map on the array with a callback that calls charCodeAt to return the character of code of character s.

Then we convert it to a hex string with toString(16).

And then we call padStart to pad the returned string to 4 characters by prepending 0’s.

Finally, we call join with an empty string to join the returned string array back to a string.

As a result, the console log logs "\u0061\u0062\u0063".

Conclusion

To convert characters to hex in JavaScript, we can use the string charCodeAt method.

Categories
JavaScript Answers

How to convert string array representation back to an array with JavaScript?

Sometimes, we want to convert string array representation back to an array with JavaScript.

In this article, we’ll look at how to convert string array representation back to an array with JavaScript.

How to convert string array representation back to an array with JavaScript?

To convert string array representation back to an array with JavaScript, we can use JSON.parse.

For instance, we write:

const s = '["item1", "item2", "item3"]'
const a = JSON.parse(s)
console.log(a)

to call JSON.parse with s to convert s back to an array.

Therefore, a is ["item1", "item2", "item3"].

Conclusion

To convert string array representation back to an array with JavaScript, we can use JSON.parse.

Categories
JavaScript Answers

How to remove certain properties from object in JavaScript?

Sometimes, we want to remove certain properties from object in JavaScript.

In this article, we’ll look at how to remove certain properties from object in JavaScript.

How to remove certain properties from object in JavaScript?

To remove certain elements from map in JavaScript, we can use some object methods and the array filter method.

For instance, we write:

const obj = {}
obj.XKey1 = "Value1";
obj.XKey2 = "Value2";
obj.YKey3 = "Value3";
obj.YKey4 = "Value4";

const entries = Object.entries(obj).filter(([key]) => key.includes('XKey'))
const newObj = Object.fromEntries(entries)
console.log(newObj)

to call Objec.entries to return an array of key-value pair arrays.

Then we call filter to check if the key string has 'XKey' in it.

Next, we call Object.fromEntries with entries to create an object from the key-value pair array returned by filter.

Therefore, newObj is

{
  XKey1: "Value1",
  XKey2: "Value2"
}

Conclusion

To remove certain elements from map in JavaScript, we can use some object methods and the array filter method.