skip to Main Content

I am using a script from the WOW Slider (free version) which is as follows:

    var slideIndex = 0;
    function showSlides() {
        var i;
        slides = document.getElementsByClassName("mySlides"); 
        dots = document.getElementsByClassName("dot");
        for (i = 0; i < slides.length; i++) {
            slides[i].style.display = "none";  
        }
        slideIndex++;
        if (slideIndex > slides.length) {slideIndex = 1}    
        for (i = 0; i < dots.length; i++) {
            dots[i].className = dots[i].className.replace(" active", "");
        }
        slides[slideIndex-1].style.display = "block";  
        dots[slideIndex-1].className += " active";
        setTimeout(showSlides, 3000); // Change image every 2 seconds
    }
    showSlides();

It seems to work OK, however when I do an SEO check on the page it says there is a Javascript error in the line:

slides[slideIndex-1].style.display = "block";

The specific message is:

“Uncaught TypeError: Cannot read property ‘style’ of undefined”.

This seems to be saying that slides is undefined, but it appears to be defined in the variable declarations.

I’m a bit of a beginner with Javascript and would therefore appreciate any help with this. Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    In case anyone is interested, I have resolved the problem by commenting out two lines of code. First I took out the line in question! When I checked all the style sheets, etc., there was no class identified as "MySlides", so I thought this is why the error message was being generated. After I took that line out the error message then refereed to the next line, which I also took out, then, with no errors being generated the slide show still works perfectly in every way! So, perhaps these were two lines of redundant code accidentally left in by the original programmer! I have to admit that I can't see how it works, but it does!


  2. Try

    <body onload="showSlides()">
    

    removing from second line on JS: showSlides()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search