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

Generating a JavaScript function call from PHP

Now that we have loaded our JavaScript file, we need a method to execute that code. Once again, we may use the Page Requirements Manager $PAGE to generate a call to our JavaScript function.

Note

This recipe describes a basic technique for executing your JavaScript code when the page is loaded. More sophisticated techniques based on handling DOM events with the Yahoo! User Interface library will be covered in later chapters.

Getting ready

Set up a page requirejs_init.php with the following content:

<?php require_once(dirname(__FILE__) . '/../config.php'); $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); $PAGE->set_url('/cook/requirejs_init.php'); $PAGE->requires->js('/cook/requirejs_init.js'); ...

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