Skip to content
Advertisement

Conditionally load and then reference external JS

I’d like to load an external JS script based on a condition (the user’s screen width), then execute a script which references that external JS.

I want to load the external JS as early as possible, so I’ve added it to the <head>, but when referencing the JS later in the <body>, it shows as undefined.

I assume this is due to the synchronous loading of JS but can’t figure how to make it work.

JavaScript

Advertisement

Answer

One possible solution I used

swiperJS.addEventListener(‘load’, callback);

to get the call back

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement