Categories
JavaScript Answers

How to display loading message when an iFrame is loading with CSS?

Sometimes, we want to display loading message when an iFrame is loading with CSS.

In this article, we’ll look at how to display loading message when an iFrame is loading with CSS.

How to display loading message when an iFrame is loading with CSS?

To display loading message when an iFrame is loading with CSS, we add a background image behind the iframe.

For instance, we write

<div class="holds-the-iframe"><iframe here></iframe></div>

to add a div with an iframe inside.

Then we style the div by writing

.holds-the-iframe {
  background: url(../images/loader.gif) center center no-repeat;
}

to set the background image to the loader icon that’s displayed when we load the iframe.

Once the iframe is loaded, the background image will be covered.

Conclusion

To display loading message when an iFrame is loading with CSS, we add a background image behind the iframe.

Categories
JavaScript Answers

How to paste as plain text in execCommand with JavaScript?

Sometimes, we want to paste as plain text in execCommand with JavaScript.

In this article, we’ll look at how to paste as plain text in execCommand with JavaScript.

How to paste as plain text in execCommand with JavaScript?

To paste as plain text in execCommand with JavaScript, we can insrt plain text with execCommand.

For instance, we write

editor.addEventListener("paste", (e) => {
  e.preventDefault();
  const text = (e.originalEvent || e).clipboardData.getData("text/plain");
  document.execCommand("insertHTML", false, text);
});

to listen for the paste event on the editor element.

In the paste event handler, we call preventDefault to prevent the default paste action.

Then we get the pasted plain text data with clipboardData.getData with "text/plain".

Finally, we use document.execCommand("insertHTML", false, text); to paste the data we got.

Conclusion

To paste as plain text in execCommand with JavaScript, we can insrt plain text with execCommand.

Categories
JavaScript Answers

How to do something N times with JavaScript?

Sometimes, we want to do something N times with JavaScript.

In this article, we’ll look at how to do something N times with JavaScript.

How to do something N times with JavaScript?

To do something N times with JavaScript, we use a for loop.

For instance, we write

const times = 10;

for (let i = 0; i < times; i++) {
  doSomething();
}

to use a for loop to loop i from 0 to 9.

We use i++ to increment i by 1 after each iteration.

And we call doSomething during each iteration.

Conclusion

To do something N times with JavaScript, we use a for loop.

Categories
JavaScript Answers

How to convert positive number to negative number in JavaScript?

Sometimes, we want to convert positive number to negative number in JavaScript.

In this article, we’ll look at how to convert positive number to negative number in JavaScript.

How to convert positive number to negative number in JavaScript?

To convert positive number to negative number in JavaScript, we can use the - operator.

For instance, we write

const n = -Math.abs(num);

to call Math.abs with num to get the absolute value of num.

Then we use the unary - operator to convert the absolute value to a negative number.

Conclusion

To convert positive number to negative number in JavaScript, we can use the - operator.

Categories
JavaScript Answers

How to convert HH:MM:SS string to seconds only in JavaScript?

Sometimes, we want to convert HH:MM:SS string to seconds only in JavaScript.

In this article, we’ll look at how to convert HH:MM:SS string to seconds only in JavaScript.

How to convert HH:MM:SS string to seconds only in JavaScript?

To convert HH:MM:SS string to seconds only in JavaScript, we can split the time string.

And then we convert the hours and minutes to seconds and add them to the seconds.

For instance, we write

const hms = "02:04:33";
const [hours, minutes, secs] = hms.split(":");
const seconds = +hours * 60 * 60 + +minutes * 60 + +secs;
console.log(seconds);

to use hms.split(":") to split the string by the colons.

And then we convert the hours string to seconds with +hours * 60 * 60.

+ converts hours to a number.

Likewise, we convert minutes to seconds with +minutes * 60.

Then we add them both to seconds.

Conclusion

To convert HH:MM:SS string to seconds only in JavaScript, we can split the time string.

And then we convert the hours and minutes to seconds and add them to the seconds.