Categories
JavaScript Answers

How to call JavaScript function in an iframe?

Sometimes, we want to call JavaScript function in an iframe.

In this article, we’ll look at how to call JavaScript function in an iframe.

How to call JavaScript function in an iframe?

To call JavaScript function in an iframe, we get the window object of the iframe with the contentWindow property.

For instance, we write

document.getElementById("resultFrame").contentWindow.Reset();

to select the iframe with getElementById.

Then we call the contentWindow.Reset global function in the iframe.

contentWindow is the window object of the iframe.

Conclusion

To call JavaScript function in an iframe, we get the window object of the iframe with the contentWindow property.

Categories
JavaScript Answers

How to clear radio button in JavaScript?

Sometimes, we want to clear radio button in JavaScript.

In this article, we’ll look at how to clear radio button in JavaScript.

How to clear radio button in JavaScript?

To clear radio button in JavaScript, we set its checked property to false.

For instance, we write

document.querySelector('input[name="Choose"]:checked').checked = false;

to select the checkbox we want to uncheck with querySelector.

And then we set its checked property to false to clear it.

Conclusion

To clear radio button in JavaScript, we set its checked property to false.

Categories
JavaScript Answers

How to get margin value of a div in plain JavaScript?

Sometimes, we want to get margin value of a div in plain JavaScript.

In this article, we’ll look at how to get margin value of a div in plain JavaScript.

How to get margin value of a div in plain JavaScript?

To get margin value of a div in plain JavaScript, we use the getComputedStyle method.

For instance, we write

const p = document.getElementById("target");
const style = window.getComputedStyle(p);

console.log(style.marginTop);

to select the element we want to get the margin for with getElementById.

Then we call getComputedStyle with p to return an object with the computed styles.

And then we get the margin top value with style.marginTop in pixels.

Conclusion

To get margin value of a div in plain JavaScript, we use the getComputedStyle method.

Categories
JavaScript Answers

How to auto start print HTML page using JavaScript?

Sometimes, we want to auto start print HTML page using JavaScript.

In this article, we’ll look at how to auto start print HTML page using JavaScript.

How to auto start print HTML page using JavaScript?

To auto start print HTML page using JavaScript, we call print when the page is loaded.

For instance, we write

window.onload = () => {
  window.print();
};

to set window.onload to a function that’s called when the page finishes loading.

In it, we call window.print to open the print dialog.

Conclusion

To auto start print HTML page using JavaScript, we call print when the page is loaded.

Categories
JavaScript Answers

How to block users from closing a window in JavaScript?

Sometimes, we want to block users from closing a window in JavaScript.

In this article, we’ll look at how to block users from closing a window in JavaScript.

How to block users from closing a window in JavaScript?

To block users from closing a window in JavaScript, we can watch the beforeunload event.

For instance, we write

window.onbeforeunload = () => {
  return "";
};

to set window.onbeforeunload to a function that’s called when the beforeunload event.

The beforeunload event is triggered when we close the window.

An alert box will show when we close the tab if we have an event listener for the beforeunload event.

Conclusion

To block users from closing a window in JavaScript, we can watch the beforeunload event.