Display random image when page loads without utilizing onload in the body tag

后端 未结 9 815
逝去的感伤
逝去的感伤 2021-01-06 06:14

I\'m trying to create a fairly simple piece of JavaScript that displays a random image from an array each time the page loads. I need to figure out a way to get this runnin

9条回答
  •  时光取名叫无心
    2021-01-06 06:53

    Wombleton's answer is what I would do. However, there is another way to do it. In the body markup, wherever you are going to put that random image, put a script that does a document.write with the markup for the image. Make sure you have an array of image URLs and substitute, like so:

    
    
    Test
    
    
    
    
    
    
    

    Again, this is not ideal, but is useful if you don't want to use any kind of onload event, not just the one you put in the tag.

提交回复
热议问题