When started from the local machine, everything is perfect. But when uploading to a web server, he does not want to work. I broke my head, I can not understand. Maybe I do not see?

UPD: Photos should be displayed in random order with each page reload.

UPD2: There is no change of images. Only the photo is visible; the path to which is registered in the img tag. The console does not issue errors.

window.onload = function () { demoFoto(); } // Предварительная загрузка изображений numimgF=0 imgslideF=new Array() imgslideF[0]=new Image() imgslideF[1]=new Image() imgslideF[2]=new Image() imgslideF[3]=new Image() imgslideF[4]=new Image() imgslideF[0].src="images/slideFoto/sf1.jpg" imgslideF[1].src="images/slideFoto/sf2.jpg" imgslideF[2].src="images/slideFoto/sf3.jpg" imgslideF[3].src="images/slideFoto/sf4.jpg" imgslideF[4].src="images/slideFoto/sf5.jpg" function demoFoto() { numimgF = Math.floor(Math.random() * (4 - 0 + 1)) + 0; document.getElementById('irfoto').src=imgslideF[numimgF].src } //////////// body <div class="rfoto"> <img align="center" class="border" height="110" id="irfoto" src="images/slideFoto/sf3.jpg" width="240" /></div> 

Sorry for bydlokod, there is no time to write humanly =)

  • So what exactly is not working? What swears? Photos are not displayed or demoFoto is not called? Describe the problem in more detail. - BOPOH

3 answers 3

Since src in img does not change at all, it means that demoFoto() does not see what to change. Perhaps it is worth trying to transfer the definition of the array to the window.onload event also before calling the function, or even to the function itself, which also allows you to write code much shorter:

 <script type="text/javascript"> window.onload = function () { demoFoto(); } function demoFoto() { img="images/slideFoto/sf"+Math.round(Math.random()*4)+".jpg"; document.getElementById('irfoto').src=img } </script> 

And check the correct relative paths to the pictures called through the script.

  • Yes, I tried to transfer to onload, it did not help. Ways checked, images available. Thank. - mccrush

Thank you all for your help. Unfortunately using native solves the problem failed.

But the solution was found with a couple of jQuery strings:

 $(document).ready(function(e) { function demoFoto() { var numimgF = Math.floor(Math.random() * (5 - 1 + 1)) + 1; $('#irfoto').attr('src','путь к файлу'+numimgF+'.jpg'); } demoFoto(); }); </script> //// body <div class="rfoto"> <img class="border" id="irfoto" src="путь к файлу/sf3.jpg" align="center" width="240" height="110"> </div> 

Here is such a strange case.

Most likely the code did not work due to an error in another script that runs earlier than this.

    The point is most likely to access the photos on the disk. Check for the existence of paths on the server. If the server is nix - there file and folder names are case sensitive. Check that the user, under which the web server works, has the right to access the folders where the pictures are located.

    • I thought about it, but the photo that is written in the img tag is displayed. The image does not change when the page is reloaded. - mccrush
    • Does irfoto src change? And why create a set of img-elements, if you do not use them later ??? For an address it would be enough to store an array of addresses. - BOPOH
    • src does not change. The set of img-elements did not play a role. I do not understand what's the matter. - mccrush
    • with your method of generating a new index picture and should not change every time you reload the page. At each index falls a range of random numbers with a length of 0.2. In my opinion, quite a lot. - uilenspiegel
    • When started from the local machine, the script works. But after uploading to the web server, no. - mccrush