Categories
JavaScript Answers

How to add CSS to the HTML head element in JavaScript?

Sometimes, we want to add CSS to the HTML head element in JavaScript.

In this article, we’ll look at how to add CSS to the HTML head element in JavaScript.

How to add CSS to the HTML head element in JavaScript?

To add CSS to the HTML head element in JavaScript, we use createElement and appendChild.

For instance, we write

const addCssToDocument = (css) => {
  const style = document.createElement("style");
  style.innerText = css;
  document.head.appendChild(style);
};

to define the addCssToDocument function.

In it, we call createElement to create a style element.

Then we set its innerText property to a css string.

Next, we call document.head.appendChild with style to append the style element has the head element’s last child.

Conclusion

To add CSS to the HTML head element in JavaScript, we use createElement and appendChild.

Categories
JavaScript Answers

How to open a popup with JavaScript and then detect when the user closes it?

Sometimes, we want to open a popup with JavaScript and then detect when the user closes it.

In this article, we’ll look at how to open a popup with JavaScript and then detect when the user closes it.

How to open a popup with JavaScript and then detect when the user closes it?

To open a popup with JavaScript and then detect when the user closes it, we can use the closed property.

For instance, we write

const winObj = window.open(
  "http://www.eexample.com",
  "google",
  "width=800,height=600,status=0,toolbar=0"
);

const loop = setInterval(() => {
  if (winObj.closed) {
    clearInterval(loop);
    alert("closed");
  }
}, 1000);

to call window.open to open a popup window.

Then we call setInterval with a callback that checks if winObj.closed is true.

If it is, then we call clearInterval to stop running the setInterval callback every second and call alert to show an alert box.

Conclusion

To open a popup with JavaScript and then detect when the user closes it, we can use the closed property.

Categories
JavaScript Answers

How to fix Syntax error: Illegal return statement in JavaScript?

Sometimes, we want to fix Syntax error: Illegal return statement in JavaScript.

In this article, we’ll look at how to fix Syntax error: Illegal return statement in JavaScript.

How to fix Syntax error: Illegal return statement in JavaScript?

To fix Syntax error: Illegal return statement in JavaScript, we should use return only in functions.

For instance, we write

const myFunction = () => {
  //...
  return myThing;
};

to define the myFunction function.

And in it, we return the myThing variable.

Conclusion

To fix Syntax error: Illegal return statement in JavaScript, we should use return only in functions.

Categories
JavaScript Answers

How to get the current minute, hour, day, week, month, year of a given millisecond time with JavaScript?

Sometimes, we want to get the current minute, hour, day, week, month, year of a given millisecond time with JavaScript.

In this article, we’ll look at how to get the current minute, hour, day, week, month, year of a given millisecond time with JavaScript.

How to get the current minute, hour, day, week, month, year of a given millisecond time with JavaScript?

To get the current minute, hour, day, week, month, year of a given millisecond time with JavaScript, we can use various date methods.

For instance, we write

const date = new Date();

const seconds = date.getSeconds();
const minutes = date.getMinutes();
const hour = date.getHours();

const year = date.getFullYear();
const month = date.getMonth();
const day = date.getDate();

const dayOfWeek = date.getDay();
const milliSeconds = date.getMilliseconds();

to get various parts of the date.

getSeconds gets the seconds of the minute.

getMinutes gets the minutes of the hour.

getHours gets the hours of the day.

getFullYear gets the 4 digit year.

getMonth gets the month of the date. 0 is January, 1 is February, etc.

getDay gets the day of the month.

getMilliseconds gets the number of milliseconds of the second.

Conclusion

To get the current minute, hour, day, week, month, year of a given millisecond time with JavaScript, we can use various date methods.

Categories
JavaScript Answers

How to create File object from Blob with JavaScript?

Sometimes, we want to create File object from Blob with JavaScript.

In this article, we’ll look at how to create File object from Blob with JavaScript.

How to create File object from Blob with JavaScript?

To create File object from Blob with JavaScript, we can use the File constructor`.

For instance, we write

const file = new File([blob], "filename");

to create a File object from a blob by putting it in an array.

The 2nd argument is a string with the file’s name.

Conclusion

To create File object from Blob with JavaScript, we can use the File constructor`.