How do I check if a module is enabled using JavaScript? I want to avoid unnecessary Ajax calls that cause 403 errors since the module is not enabled.

if (mymodule_exist) {
  // Make an Ajax call.
    url: "/player/status",
    type: "POST",
    async: false,
    success: function(data) {
      player_value = data;

I am trying to do something like this. How do I do it efficiently?

  • Are you sure it's the simplest way for fix your problem ? Can you just not add the AJAX part in your module if the require module isn't enabled ? Or can you just add the require module in the .info file of your module ? Hope that help. May 6, 2014 at 11:27
  • @NicolasPERNOT : Can you just not add the AJAX part in your module if the require module isn't enabled - I dont think we can make ajax call in module, ajax call is made only in JS , AM i right? ... what are you trying to say
    – Hitesh
    May 6, 2014 at 12:17
  • 3
    I believe what Nicolas PERNOT is saying is: Construct a module to output the ajax call - have it check to see if the appropriate module exists, if it does it would output the code, if not, no output. This has the added benefit of not poluting the DOM with unnecessary markup and code.
    – Geoff
    May 6, 2014 at 13:12
  • @Geoff : Thanks !!! that does make sense ... but which one is better ?
    – Hitesh
    May 6, 2014 at 13:20
  • The approach that NicolasPERNOT is suggesting would be best I suspect. Since it defers the workload onto the server from the clients computer - this should result in faster, smoother page loads without unusable markup.
    – Geoff
    May 6, 2014 at 23:14

2 Answers 2


You can check module is enabled or not using module_exists function... Ideally your .module which checks for above condition and sets variable for JavaScript file goes below..

See detailed steps below on adding this condition

  • Create a folder called 'custom' in sites/all/modules
  • Create a folder called 'mycustomjs' in above custom folder
  • Create a file called 'mycustomjs.info' inside above folder with

below code

name = My Custom JS Alters
description = A custom module to interact with JS
core = 7.x
package = Custom

Create a file called mycustomjs.module inside 'mycustomjs' folder with below code


function mycustomjs_preprocess_page(&$vars) {
  $module = "MODULENAME"; // Change modulename to module you want to verify
  $does_module_enabled = module_exists($module);
  if ($does_module_enabled) {
    drupal_add_js(array('MODULENAME' => array('enabled' => TRUE)), 'setting');
  else {
    drupal_add_js(array('MODULENAME' => array('enabled' => FALSE)), 'setting');


For more information See the Creating Drupal 7.x modules if you're not sure

  • great!!! I am liitle new to drupal so I have one question mycustomjs_preprocess_page can be changed to any function name like 'my_function_setting_variable' or it has to follow the my_module name , ?
    – Hitesh
    May 6, 2014 at 14:08
  • It always should by MODULENAME_preprocess_page ... So mycustomjs_preprocess_page .. see more about preprocess functions here
    – Anil Sagar
    May 6, 2014 at 14:24
  • 1
    Thanks my mistake was I was making custom function function mysettings() and was getting error Uncaught ReferenceError: jQuery is not defined I changed it to HOOK_PREPROCESSOR_PAGE as you suggessted and also got some help from here drupal.stackexchange.com/a/76565/26318 and drupal.stackexchange.com/questions/74708/… for help ... now I am going to try calling setting variable in JS side :)
    – Hitesh
    May 6, 2014 at 14:28

I can think of two ways:

  • Send a JavaScript flag as settings, indicating the module is active:

    drupal_add_js(array('my_module' => array('enabled' => TRUE)), 'setting');
  • If the module is enabled, modify the output in some way so the JavaScript code that makes the petition is included only when your module is enabled, or the contrary way, make sure that code is not included if your module is disabled: you will have to use another module or some kind of configuration. The exact way depends on the origin of the JavaScript code that makes the Ajax petitions.

  • I understood the first point and it makes sense, can you elaborate second point please. thanks
    – Hitesh
    May 6, 2014 at 12:10
  • Even First point should be done within some module !!! Am I correct ? and if "mymodule" is not even installed in my certain server than I need to keep this drupal_add_js(array('my_module' => array('enabled' => TRUE)), 'setting'); in another common module my_common_module of my site ....... can you tell me in that case what will be the correct way for this 1. drupal_add_js(array('my_module' => array('enabled' => TRUE)), 'setting'); or 2.drupal_add_js(array('my_common_module' => array('enabled' => TRUE)), 'setting');
    – Hitesh
    May 6, 2014 at 12:13
  • Well, it depends on the origin of code that makes the AJAX petition. The idea is to avoid this code to be sent to the client if your module is not enabled (you will need another module or some configuration) or add this code if your module is enabled. The eactly way of doing it depends on the irigin of that code. I've edited my answer to reflect this.
    – sanzante
    May 6, 2014 at 12:14
  • ;) Same question asked in next comment !!! Please edit your answer :) it will really be helpful , also if you can add some helpful links it would be great help ...Thanks
    – Hitesh
    May 6, 2014 at 12:14
  • I can't give you more info with more details about the origin of that petitions.
    – sanzante
    May 6, 2014 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.