Categories
JavaScript Answers

How to detect if a browser is blocking a popup with JavaScript?

Sometimes, we want to detect if a browser is blocking a popup with JavaScript.

In this article, we’ll look at how to detect if a browser is blocking a popup with JavaScript.

How to detect if a browser is blocking a popup with JavaScript?

To detect if a browser is blocking a popup with JavaScript, we can check the closed property of the window.

For instance, we write

const newWin = window.open(url);

if (!newWin || newWin.closed || typeof newWin.closed == "undefined") {
  //...
}

to call window.open to open the url.

Then we check if the window object’s closed property is true or undefined or if the window object is falsy.

If any of these are true, then the window is blocked from opening.

Conclusion

To detect if a browser is blocking a popup with JavaScript, we can check the closed property of the window.

Categories
JavaScript Answers

How to detect browser version and operating system using JavaScript?

Sometimes, we want to detect browser version and operating system using JavaScript.

In this article, we’ll look at how to detect browser version and operating system using JavaScript.

How to detect browser version and operating system using JavaScript?

To detect browser version and operating system using JavaScript, we an use the platform.js library.

To install it, we run

npm i --save platform

Then we use it by writing

const platform = require("platform");

console.log(platform.name);
console.log(platform.version);
console.log(platform.layout);
console.log(platform.os);
console.log(platform.description);

We get the browser name with platform.name.

platform.version has the browser version.

platform.layut has the layout engine name.

platform.os has the OS name.

platform.description has the description string of the browser engine.

Conclusion

To detect browser version and operating system using JavaScript, we an use the platform.js library.

Categories
JavaScript Answers

How to get element at specified position with JavaScript?

Sometimes, we want to get element at specified position with JavaScript.

In this article, we’ll look at how to get element at specified position with JavaScript.

How to get element at specified position with JavaScript?

To get element at specified position with JavaScript, we can use the elementFromPoint or elementsFromPoint methods.

For instance, we write

document.elementFromPoint(x, y)

to get the element at point with xy coordinates x and y.

And we write

document.elementsFromPoint(x, y)

to get all elements at point with xy coordinates x and y.

Conclusion

To get element at specified position with JavaScript, we can use the elementFromPoint or elementsFromPoint methods.

Categories
JavaScript Answers

How to add a transparent background with three.js and JavaScript?

Sometimes, we want to add a transparent background with three.js and JavaScript.

In this article, we’ll look at how to add a transparent background with three.js and JavaScript.

How to add a transparent background with three.js and JavaScript?

To add a transparent background with three.js and JavaScript, we create a rendered object and call setClearColor on it.

For instance, we write

const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setClearColor(0x000000, 0);

to create a renderer with the THREE.WebGLRenderer constructor.

Then we call setClearColor to add a transparent background.

Conclusion

To add a transparent background with three.js and JavaScript, we create a rendered object and call setClearColor on it.

Categories
JavaScript Answers

How to submit a form with JavaScript by clicking a link?

Sometimes, we want to submit a form with JavaScript by clicking a link.

In this article, we’ll look at how to submit a form with JavaScript by clicking a link.

How to submit a form with JavaScript by clicking a link?

To submit a form with JavaScript by clicking a link, we can listen to the click event of the button.

For instance, we write

<form id="form-id">
  <button id="your-id">submit</button>
</form>

to add a form.

Then we write

const form = document.getElementById("form-id");

document.getElementById("your-id").addEventListener("click", () => {
  form.submit();
});

to select the form with getElementById.

Then we select the button with

document.getElementById("your-id")

And then we call addEventListener to add a click event listener to the button.

In the click event handler callback, we call form.submit to submit the form.

Conclusion

To submit a form with JavaScript by clicking a link, we can listen to the click event of the button.