Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:22:37+00:00 2026-06-13T10:22:37+00:00

I have a moodle block plugin where I’m trying to include javascript in the

  • 0

I have a moodle block plugin where I’m trying to include javascript in the page.

No matter how I try, the javascript called by the js_init_call always throws an error instead of running I’ve tried passing a full array to js_init_call, and I’ve tried the shorthand form that assumes the file is called module.js

If the javascript file doesn’t exist, I get a moodle error complaining about the missing file. If it does exist, I get a javascript error as above. What am I doing wrong?

block_foo.php:

<?php
class block_foo extends block_base {
    public function init() {
    $this->title = get_string('foo', 'block_foo');
    }

    public function applicable_formats(){
        return array('course-view' => true);
    }

    public function get_content() {
        global $PAGE;
        if ($this->content !== null) {
          return $this->content;
        }

        if(!isloggedin()){
        //if(!is_enrolled()){
            return false;
        }
        $this->content         =  new stdClass;
        $this->content->text = 'asdf';
        $this->content->footer = '';

        $jsmodule = array(
            'name'     => 'block_foo',
            'fullpath' => '/blocks/foo/foo.js',
            'requires' => array(),
            'strings' => array()
        );
        $PAGE->requires->js_init_call('M.block_foo.init', null, false, $jsmodule);

        //This style of call doesn't work either... (if the js is named module.js)
        //$PAGE->requires->js_init_call('M.block_foo.init', null);

        return $this->content;
    }

    function hide_header(){
        return true;
    }
}

module.js or foo.js:

M.block_foo = {};
M.block_foo.init = function(){
    alert("I was called, yay");
    $var = 1234;
    $var = $var + 3;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:22:38+00:00Added an answer on June 13, 2026 at 10:22 am

    This problem, ultimately, was caused by Moodle responding with a HTTP/200 response of size 0 to requests for the javascript file.

    That response appeared to have been caused by the module being just a symbolic link to my development directory instead of an actual copy of the files. Why this only happened for the javascript file, and not the php files, I don’t know.

    So don’t develop moodle modules then symbolic-link them into place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on Moodle 2.2.1 and have created dynamic.csv files using certain queries.
I have been experimenting with a lot of web development apps like Drupal, Moodle,
Have a painfully simple blog Post creator, and I'm trying to check if the
have 2 questions : A computer with 32-bit address uses 2-level page table (9
I have a project that uses the moodle library. I had to change the
I have my moodle setup using this guide ( Ubuntu 11.04 ) http://docs.moodle.org/dev/Install_Moodle_On_Ubuntu_with_Nginx/PHP-fpm I
I am really a newbie to Moodle and I have got this advanced functionality
I have some data in a old MSSQL 2000 db. I am trying to
I have used this plugin before but now the blockui is not positioning correctly
I have a Moodle installation integrated with LDAP and based on that it makes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.