tags:

views:

98

answers:

3

I am making a drupal module. My requirement in the module is that when the module is activated then it needs to add a javascript in page.tpl file just after the body tag. Can anyone help me how to do this. I have tried using drupal_add_js but it will not exactly put the script after body tag, rather puts inside the head or below in footer. I also tried using template variable and preprocess method but the problem is the preprocess method replaces the old value of that variable with the new one. Is there a way how i can implement this in.

A: 

You could implement a yourModule_preprocess_page(&$vars) function within your module that adds your custom JavaScript inclusion markup as a new entry to the $vars array, e.g. as $vars['yourModule_js']:

function yourModule_preprocess_page(&$vars) {
  $vars['yourModule_js'] = yourModule_create_js_markup(); // TODO: adjust to your needed output
}

With this in place, you modify your page.tpl.php file to check for the existence of this variable, and if it is there, output it wherever you like:

<?php if ($yourModule_js): ?>
  <?php print $yourModule_js; ?>
<?php endif; ?>

NOTE: This is not a recommended way to go, as it will exclude your JavaScript from the Drupal caching/aggregation mechanism and might easily cause problems/conflicts with other JavaScript on your site. You should consider adjusting your JavaScript to work in a Drupal conform way, see Overview of JavaScript, AJAX, AHAH API and the pages linked from JavaScript in Drupal for details on this.

Henrik Opel
I have used this method but the problem is in my requirement for the module i am not allowed to change the page.tpl. The module should automatically do during the activation. It's more like doing everything for drupal User interface.
prabdrup
@prabdrup: Without changing page.tpl.php, you have no control whatsoever where any changes you make will be printed, or even if they get printed at all. Be aware that the page.tpl.php file can be quite different depending on the theme used, and the author of the file is completely free to skip printing even the standard Drupal variables. I see no way to fulfill this specific requirement in Drupal.
Henrik Opel
A: 

Drupal has no API to allow for adding JS Just anywhere.

Simplest solution is to add it -manually, hardcoded- in page.tpl.php in your theme. If you do not like that, you can continue on this route:

  • Add a variable in page.tpl.php on the place where you would like the Js to be printed
  • With variable preprocessing, initialise a variable for 'page' *$vars['inline_js']*
  • Simplest way to fill that variable, is to assign it a hardcoded value (ugly)
  • Slighly harder, but still simple, is to assign it a variable: variable_get('inline_js', '...my_inline_js'). You can then set this variable in your settings.php, or write a very simple module to set it.
berkes
slighly harder, but still simple, is to assign it a variable: variable_get('inline_js', '...my_inline_js'). You can then set this variable in your settings.php, or write a very simple module to set it.Do we have to modify page.tpl for this? I am looking for something that will do everything after activating the module . User should not go to page.tpl and change the code. Everything should work on activation on module.
prabdrup
Yes, you need to modify page.tpl.php.AFAIK you cannot -ever- set Js after the body, since Drupal has no variable to print there. So AFAIK what you want is not possible.Maybe you can abuse a variable that is printed in $page, but that would be just that: ABUSE. And it will break.
berkes
@berkes: the function `drupal_add_js()` exists for a precise purpose (to include a JavaScript file in the page being viewed). It's not correct to say that is not possible to do what asked without to alter the template files; all modules that need to include a JavaScript file don't alter the page template at all.
kiamlaluno
@kiamlaluno, sorry, you are misunderstood. The poster wants to put Js INSIDE the body tag. Drupal add js only allows inserting js in predefined places: the HTML header, footer or other defined regions. As such: there is no region directly after the BODY tag. So if you want to place JS there, you need to either introduce a region there, or print a JS variable there.
berkes
@berkes: `drupal_add_js($script_code, 'module', 'footer')`. The footer is always added before the end of the body tag.
kiamlaluno
I know, prabdrup asked to insert it right after the beginning of the body tag; as that is not possible, I think it's more acceptable to place the JavaScript code in the footer, rather than changing a theme template. The pro of adding it with `drupal_add_js()` is that the code will always be added, even if the used theme is changed, or is updated.
kiamlaluno
+1  A: 

i think the best way to go is to put it in the footer like this, depending on the theme it will be rendered at the end of the page, which also means getting evaluated very late, which is why you'd want to put JS at the bottom:

$path_to_script = $drupal_get_path('module', 'my_module') . '/my_module.js');
drupal_add_js($path_to_script, $type = 'module', $scope  = 'footer');
eikes

related questions