Adding a JavaScript File

I am trying to insert a link into a Javascript file in the header using drupal_add_js(). I placed this line inside the template preprocessor function in template.php. As a result, the code does not work at all: there is no script link in the output, as it should be. Can someone tell me what I am doing wrong?

function phptemplate_preprocess_page(&$vars) {
    $url = drupal_get_path("theme","mysite");  
    drupal_add_js($url."/jquery.js");  
    drupal_add_js($url."/drupal.js");  

.....
+5
source share
5 answers

drupal_add_js()works, but you put it in the page rendering process. I suggest you put it in template.php, as you do, but at the beginning, outside of any function. This is what we have done in several of our projects.

-1
source

, Javascript, , .info. . http://drupal.org/node/171205#scripts.

+10
  drupal_add_js(path_to_theme().'/js/jquery.cycle.all.js');
  $vars['scripts'] = drupal_get_js();
+2

javascript , .info. .

scripts[] = myJavaScriptFile.js

, , , .

+1

, drupal_add_js() hook_preprocess_page() . JavaScript, drupal_add_js(), $scripts template_preprocess_page(), hook_preprocess_page() template_preprocess_page(). , , drupal_add_js() .tpl.php (), $scripts:

function THEME_preprocess_page(&$variables)
  drupal_add_js(...);
  $variables['scripts'] = drupal_get_js();
}

But you do not need to add jquery.jsit drupal.jsyourself, it should be done automatically using the Drupal core. If you need to do this yourself, something will break on your site. You can (re) add files as a quick fix, but it's better to find the root cause of the problem, as this most likely creates other problems that you have not yet identified (or did not work without realizing it).

+1
source

All Articles