Slider with JS only works in chrome

0

I'm doing an automatic slider and in chrome it works for me but in mozilla and opera no image comes out. With this I rule out that I am calling the document wrong but I can not see the error, can you help me?

window.addEventListener('load', function (){
        
    var imagenes = [];
    imagenes[0] = '../Imagenes/slider1.jpg';
    imagenes[1] = '../Imagenes/slider2.jpg';
    imagenes[2] = '../Imagenes/slider3.jpg';
    var long_imagenes = imagenes.length;

    var cnt = 0;
    function cambiarImagenes(){


        document.img_slider.src = imagenes[cnt];

        if(cnt < (long_imagenes-1)){
            cnt = cnt +1;
        }else{
            cnt = 0;
        }
    }

    setInterval(cambiarImagenes,3000);
});
<div id="slider_cont">
    <img name="img_slider" id="img_slider">
</div>

EDITO Thanks to the comments so far I think the error is in the route, because if I put the links there is the snippet if it works.

My root:

What I do is leave the folder js, enter images and define the image I want to take.

    
asked by NEA 17.10.2018 в 09:40
source

2 answers

0

I already found the error, it is the wrong route. Well, even though I'm inside a js file, I'm actually modifying html .

I had:

imagenes[0] = '../Imagenes/slider1.jpg';
imagenes[1] = '../Imagenes/slider2.jpg';
imagenes[2] = '../Imagenes/slider3.jpg';

And it is:

imagenes[0] = 'Imagenes/slider1.jpg';
imagenes[1] = 'Imagenes/slider2.jpg';
imagenes[2] = 'Imagenes/slider3.jpg';

Many thanks to the previous help, thanks to them I have been able to find the error.

    
answered by 17.10.2018 / 18:42
source
2

Although it works perfectly for me in Firefox (Ubuntu version), I suggest you try replacing

document.img_slider.src = imagenes[cnt];

for

let slider = document.getElementById('img_slider');
slider.src = imagenes[cnt];

What is the standard for finding items in the current document.

    
answered by 17.10.2018 в 11:10