Categories
JavaScript Answers

How to conditionally import an ES6 module with JavaScript?

Sometimes, we want to conditionally import an ES6 module with JavaScript.

In this article, we’ll look at how to conditionally import an ES6 module with JavaScript.

How to conditionally import an ES6 module with JavaScript?

To conditionally import an ES6 module with JavaScript, we can use the import function.

For instance, we write

const myModule = await import(moduleName);

in an async function to call import with the moduleName string to import the module named moduleName.

And then we get the module returned by the promise returned by import with await.

Conclusion

To conditionally import an ES6 module with JavaScript, we can use the import function.

Categories
JavaScript Answers

How to refresh part of page with JavaScript?

Sometimes, we want to refresh part of page with JavaScript.

In this article, we’ll look at how to refresh part of page with JavaScript.

How to refresh part of page with JavaScript?

To refresh part of page with JavaScript, we can set the innerHTML property of the element we want to update to the content we want to show.

For instance, we write

const url =
  "https://server.test-cors.org/server?id=2934825&enable=true&status=200&credentials=false&methods=GET";

async function refresh() {
  btn.disabled = true;
  dynamicPart.innerHTML = "Loading...";
  dynamicPart.innerHTML = await (await fetch(url)).text();
}

to create the refresh function that makes a GET request to the url with fetch.

And then we get the text content from the response and set the innerHTML property of the dynamicPart element to what we want.

The dynamicPart element may be an element like

<div id="dynamicPart">Dynamic part</div>

We can select it with

const dynamicPart = document.getElementById('dynamicPart')

Conclusion

To refresh part of page with JavaScript, we can set the innerHTML property of the element we want to update to the content we want to show.

Categories
JavaScript Answers

How to check if an element is really visible with JavaScript?

Sometimes, we want to check if an element is really visible with JavaScript.

In this article, we’ll look at how to check if an element is really visible with JavaScript.

How to check if an element is really visible with JavaScript?

To check if an element is really visible with JavaScript, we can check if the element’s clientHeight is bigger than 0.

For instance, we write

const isViewable = (element) => {
  return element.clientHeight > 0;
};

to create the isViewable function to check if element.clientHeight is bigger than 0.

If clientHeight is bigger than 0, then the element is visible since clientHeight includes padding and margin.

Conclusion

To check if an element is really visible with JavaScript, we can check if the element’s clientHeight is bigger than 0.

Categories
JavaScript Answers

How to split a long array into smaller arrays with JavaScript?

Sometimes, we want to split a long array into smaller arrays with JavaScript.

In this article, we’ll look at how to split a long array into smaller arrays with JavaScript.

How to split a long array into smaller arrays with JavaScript?

To split a long array into smaller arrays with JavaScript, we can use the array splice method.

For instance, we write

const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
const b = a.splice(0, 10);

to call splice with 0 and 10 to remove the first 10 items from array a and return them in a new array.

Therefore, a is [11, 12, 13, 14, 15] and b is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] after calling a.splice.

Conclusion

To split a long array into smaller arrays with JavaScript, we can use the array splice method.

Categories
JavaScript Answers

How to get scroll bar width using JavaScript?

Sometimes, we want to get scroll bar width using JavaScript.

In this article, we’ll look at how to get scroll bar width using JavaScript.

How to get scroll bar width using JavaScript?

To get scroll bar width using JavaScript, we can subtract the scrolling element’s width by the immediate child’s width.

For instance, we write

const child = document.querySelector("div");
const scrollbarWidth = child.parentNode.offsetWidth - child.offsetWidth;

to get the scroll container’s width with child.parentNode.offsetWidth .

Then we get the child div’s width with child.offsetWidth.

And then we subtract the scroll container’s width with the div’s width to get the scroll bar’s width.

Conclusion

To get scroll bar width using JavaScript, we can subtract the scrolling element’s width by the immediate child’s width.