0

I have this code in my javascript:

(function ($) {  
    Drupal.behaviors.myid_print_table_id_link = {
        attach: function (context, settings) { 
            $.ajax({
              type: 'POST', 
              url: '?q=myid_print/table_pager',                                      
              success: function(data) {
                console.log(data);                          
              }                                                 
            }).done(function(o) {                  
              console.log(o);                                
            });
        }
    }  
})(jQuery);

This calls a function called myid_print_table_pager declared in my hook menu:

 function myid_print_menu(){

     $items = array();
     $items['myid_print/table_pager'] = array(
         'title' => 'Table Pager',
         'page callback' => 'myid_print_table_pager',       
         'type' => MENU_CALLBACK,
     ); 
     return $items;
  }
 }

Below is my function:

function myid_print_table_pager(){
    drupal_add_js('$(document).ready(function () { alert("Hello!"); });', 'inline');
    watchdog('message', 'I've been here!');
}

I'm pretty sure that the function myid_print_table_pager is invoke because 'I've been here!' is seen in my watchdog table in my database. But why does the alert with the message Hello! has not been invoke?

5
  • Try this: drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline'); $ is just an alias for jQuery. Maybe it is overwritten.
    – Huelfe
    Sep 21, 2015 at 7:38
  • @Huelfe --> That doesnt work either...
    – user46175
    Sep 21, 2015 at 7:41
  • 2
    Maybe you should try to return something from your page callback, like return array('#markup' => 'Hello world');. I'm not sure if Drupal handles the page correctly if you don't provide the main page content. Sep 21, 2015 at 7:43
  • @marcvangend --> How will I do that exactly?
    – user46175
    Sep 21, 2015 at 7:47
  • Just add that line at the end of the page callback. See my answer below also. Sep 21, 2015 at 8:07

3 Answers 3

3

I usually avoid calling drupal_add_js() and use the #attached property of the render array. The advantage of using #attached is that you don't need to implement additional hooks, the js is only added when needed, and (as I've been told) it works better with caching. Your code would look something like this:

function myid_print_menu() {
  $items = array();
  $items['myid_print/table_pager'] = array(
    'title' => 'Table Pager',
    'page callback' => 'myid_print_table_pager',
    'type' => MENU_CALLBACK,
  );
  return $items;
}


function myid_print_table_pager() {
  $build = array(
    '#markup' => 'Hello world!',
    '#attached' => array(
      'js' => array(
        array(
          'data' => '$(document).ready(function () { alert("Hello!"); });',
          'type' => 'inline',
        ),
      ),
    ),
  );
  return $build;
}

Disclaimer: untested code, please let me know if it doesn't work as expected.

0

You'ld better use hook_page_build to add your JS.

/**
 * Implements hook_page_build().
 */
function MYMODULE_page_build(&$page) {
  drupal_add_js('
    jQuery(document).ready(function () {

      console.log("Hello!");
      // Do your magic here:

    });', 'inline');
}
4
  • What I really want is to invoke a function, inline.
    – user46175
    Sep 21, 2015 at 7:32
  • @Aliyah – Updated answer.
    – leymannx
    Sep 21, 2015 at 7:40
  • Not working either, I just put myid_print_page_build. I want to alert Hello after an ajax call. Where am I missing?
    – user46175
    Sep 21, 2015 at 7:44
  • Well, that's no Drupal Question then. Better ask that on SO.
    – leymannx
    Sep 21, 2015 at 7:47
0

Try

function myid_print_table_pager(){
     drupal_add_js('(function($){$(document).ready(function(){alert("Hello!");});})(jQuery);', 'inline');
     watchdog("message", "I've been here!");
}

And

(function ($) {  
    $(document).ready(function() {
        Drupal.behaviors.myid_print_table_id_link = {
            attach: function (context, settings) { 
                $.ajax({
                  type: 'POST', 
                  url: '?q=myid_print/table_pager',                                      
                  success: function(data) {
                    console.log(data);                          
                  }                                                 
                }).done(function(o) {                  
                  console.log(o);                                
                });
            }
        }  
    });
})(jQuery);

Your Answer

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