I have a code which add a class on my navbar, this class will be added only on specific pages. I check which pages like this pathName= window.location.pathname.split('/'); this code returns me an array with the splited path, then I use the foreach loop to check if this page matches the requirements. If it does, the path will be undefined. But my if statment doesn't work because, I get this error Cannot read properties of undefined (reading '2')
The method where I'm getting a path
let pathName;
$(window).on('load', function () {
if($('.owl-item').length<5){
}
pathName= window.location.pathname.split('/');
}
My scrolling method
$(window).scroll(function (evt) {
if(pathName[2]===undefined) the line where I getting the error{
**something**
}else{
**something**
}
}
I have solved the problem I changed my if statment to this
Before
$(window).scroll(function (evt) {
if(pathName[2]===undefined) **this** {
**something**
}else{
**something**
}
}
After
$(window).scroll(function (evt) {
if(typeof pathName[2]==='undefined') **to this**{
**something**
}else{
**something**
}
}