- Random Picture Rotation with JavaScript
- How the Rotating Images Code Works
- The Coding Plan
- Some Key JavaScript Concepts
- Detecting the Load Event
- Triggering the Function from the Load Event
- Generating Random Numbers
- Creating the Image Path
- DOM Scripting
- Assembling the Image Element
- Adding the Image Element to the Page
- Reviewing the Working Page
- Summary
Detecting the Load Event
We want our code to add an image to the web page. To do this, we must modify the Document Object Model (DOM), which is the browser's view of your markup.
Fortunately for us, the browser is constantly broadcasting messages telling JavaScript about various page and user events, such as mouse clicks and key presses. One of these events, load, is fired when the document is fully loaded in the browser. That's the moment we want to call a JavaScript function that does the work of selecting and adding the image to the page.