if you’re running scripts directly in the browser and want to load another JavaScript file dynamically, you can create a script element and append it to the document.
Here’s how:
function loadScript(url, callback) {
const script = document.createElement('script');
script.src = url;
script.onload = () => callback && callback();
document.head.appendChild(script);
}
// Usage:
loadScript('utils.js', () => {
console.log('utils.js loaded!');
// You can now use functions or variables from utils.js here
});
This method doesn’t require ES modules and works in all browsers. It’s handy if you want to load scripts conditionally or after some event.