O'Reilly logo

Moodle JavaScript Cookbook by Alastair Hole

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Attaching advanced DOM event handlers

Sometimes we simply want to run JavaScript code when the page loads, rather than in response to an action taken by the user (as in the previous example).

A simple way to achieve this would be to put our code directly in the JavaScript file outside of any functions or event handlers. Using this method, our code would just be executed straight after it is loaded. If we are following the best practice technique of loading our script files at the end of the body tag, our code will be executed after the main body content of the page has loaded.

Note that Moodle implements this best practice for us automatically when we use the technique from the Loading a JavaScript file recipe in Chapter 1, Combining Moodle and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required