I have a website with several pages. there is a Swiper slider on the homepage. all pages have a main.js file where the slider is initialized. how do I make sure that the other pages don't get an error in the console?
Error:
Uncaught ReferenceError: Swiper is not defined
Looking into the Swiper API, you can see that it uses a class called Swiper to initialize the slider.
Since you just want to validate if the Swiper class is present in your page, you can simple test it's type:
if (typeof Swiper != undefined) {
const swiper = new Swiper('.swiper');
}