Categories
JavaScript Answers

How to intercept page exit event with JavaScript?

Sometimes, we want to intercept page exit event with JavaScript.

In this article, we’ll look at how to intercept page exit event with JavaScript.

How to intercept page exit event with JavaScript?

To intercept page exit event with JavaScript, we listen for the beforeunload event.

For instance, we write

window.onbeforeunload = (e) => {
  //...
  return "Your confirmation message goes here.";
};

to set window.onbeforeunload to a function to add an event listener for the beforeunload event.

We return something non null in the function to show a message box when the user tries to exit the page.

The message in the box can’t be changed.

Conclusion

To intercept page exit event with JavaScript, we listen for the beforeunload event.

Categories
JavaScript Answers

How to set additional data to HighCharts series with JavaScript?

Sometimes, we want to set additional data to HighCharts series with JavaScript.

In this article, we’ll look at how to set additional data to HighCharts series with JavaScript.

How to set additional data to HighCharts series with JavaScript?

To set additional data to HighCharts series with JavaScript, we can add the tooltip.formatter method and the series array.

For instance, we write

const chartData = [
  { timestamp: 1515059819853, value: 1, somethingElse: "foo" },
  { timestamp: 1515059838069, value: 2, somethingElse: "bar" },
  { timestamp: 1515059838080, value: 3, somethingElse: "baz" },
  // ...
];

const Chart = Highcharts.stockChart(myChart, {
  // ...options
  tooltip: {
    formatter() {
      const pointData = chartData.find((row) => row.timestamp === this.point.x);
      // ...
    },
  },
  series: [
    {
      name: "Numbers over the course of time",
      data: chartData.map((row) => [row.timestamp, row.value]),
    },
  ],
});

to create a stock chart with Highcharts.stockChart.

In the object in the 2nd argument, we add the tooltip.formatter method to find the pointData for the point with the data from this.point.x x coordinate value.

And then we return the text for the tooltip.

In series we add an array with the data form the x and y axes.

Conclusion

To set additional data to HighCharts series with JavaScript, we can add the tooltip.formatter method and the series array.

Categories
JavaScript Answers

How to fix canvas.toDataURL() SecurityError with JavaScript?

Sometimes, we want to fix canvas.toDataURL() SecurityError with JavaScript.

In this article, we’ll look at how to fix canvas.toDataURL() SecurityError with JavaScript.

How to fix canvas.toDataURL() SecurityError with JavaScript?

To fix canvas.toDataURL() SecurityError with JavaScript, we set the crossOrigin attribute to 'anonymous' with setAttribute.

For instance, we write

const img = new Image();
img.setAttribute("crossOrigin", "anonymous");
img.src = url;

to create an image with Image.

Then we set the crossOrigin attribute to 'anonymous' with

img.setAttribute("crossOrigin", "anonymous");

Then we set the img.src to the url that’s returned by canvas.toDataURL.

This works if we have the correct permission to the images in the canvas.

Conclusion

To fix canvas.toDataURL() SecurityError with JavaScript, we set the crossOrigin attribute to 'anonymous' with setAttribute.

Categories
JavaScript Answers

How to check if a font (@font-face) has already been loaded with JavaScript?

Sometimes, we want to check if a font (@font-face) has already been loaded with JavaScript.

In this article, we’ll look at how to check if a font (@font-face) has already been loaded with JavaScript.

How to check if a font (@font-face) has already been loaded with JavaScript?

To check if a font (@font-face) has already been loaded with JavaScript, we can listen to a few events emitted by document.fonts.

For instance, we write

document.fonts.onloading = () => {
  // ...
};

document.fonts.onloadingdone = () => {
  // ...
};

document.fonts.onloading = () => {
  // ...
};

to run the document.fonts.onloading function when fonts begin to download.

document.fonts.onloadingdone method runs when fonts are loaded completely.

And document.fonts.onloading runs when fonts failed to load.

We can also use await document.fonts.ready to wait for fonts to load.

Conclusion

To check if a font (@font-face) has already been loaded with JavaScript, we can listen to a few events emitted by document.fonts.

Categories
JavaScript Answers

How to get selected option text with JavaScript?

Sometimes, we want to get selected option text with JavaScript.

In this article, we’ll look at how to get selected option text with JavaScript.

How to get selected option text with JavaScript?

To get selected option text with JavaScript, we can use the text property of the selected option element.

For instance, we write

const onChange = (e) => {
  console.log(e.target.options[e.target.selectedIndex].text);
};

document.getElementById("box1").onchange = onChange;

to create the onChange function that gets the selected option with

e.target.options[e.target.selectedIndex]

And then we get the text of the selected option with text.

Then we set document.getElementById("box1").onchange to onChange to use that as the listener that runs when we change options.

Then we add the drop down with

<select id="box1">
  <option value="98">dog</option>
  <option value="7122">cat</option>
  <option value="142">bird</option>
</select>

Conclusion

To get selected option text with JavaScript, we can use the text property of the selected option element.