Categories
JavaScript Answers

How to scroll an iframe with JavaScript?

Sometimes, we want to scroll an iframe with JavaScript

In this article, we’ll look at how to scroll an iframe with JavaScript.

How to scroll an iframe with JavaScript?

To scroll an iframe with JavaScript, we can use the scrollTo method.

For instance, we write

const myIframe = document.getElementById("iframe");

myIframe.onload = () => {
  myIframe.contentWindow.scrollTo(xcoord, ycoord);
};

to get the iframe with getElementById.

Then we set its onload property to a function that scrolls itself to the x-y coordinates xcoord and ycoord with scrollTo.

We get the iframe window with myIframe.contentWindow.

Then we call scrollTo on the window.

Conclusion

To scroll an iframe with JavaScript, we can use the scrollTo method.

Categories
JavaScript Answers

How to format date to MM/dd/yyyy in JavaScript?

Sometimes, we want to format date to MM/dd/yyyy in JavaScript.

In this article, we’ll look at how to format date to MM/dd/yyyy in JavaScript.

How to format date to MM/dd/yyyy in JavaScript?

To format date to MM/dd/yyyy in JavaScript, we can use some date and string methods.

For instance, we write

const getFormattedDate = (date) => {
  let year = date.getFullYear();
  let month = (1 + date.getMonth()).toString().padStart(2, "0");
  let day = date.getDate().toString().padStart(2, "0");
  return month + "/" + day + "/" + year;
};

to create the getFormattedDate function.

In it, we get the 4 digit year from the date with getFullYear.

getMonth gets the month.

getDate gets the date of the month.

We call padStart to prepend 0 to pad the string to 2 digits if the number has less than 2 digits.

And then we return the month, day and year combined into a new string.

We add 1 to the month returned by getMonth to make it a human readable month number.

Conclusion

To format date to MM/dd/yyyy in JavaScript, we can use some date and string methods.

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.