Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a JavaScript file

I'm trying to insert reference to the Javascript file in the header by using drupal_add_js(). I placed this line inside the template preprocess function in template.php. The result that the code is not working at all: There is no script link in output as it should be. Can anyone tell me what am I 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");  

.....
like image 955
Andrew Avatar asked Apr 01 '10 21:04

Andrew


People also ask

Where do I put JavaScript files?

JavaScript in <head> or <body> You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Can I add JavaScript to HTML file?

Adding JavaScript into an HTML Document You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.

How do I run a JavaScript file?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.


1 Answers

Even easier, Javascript that needs to be loaded on all pages can be added in the theme's .info file. See http://drupal.org/node/171205#scripts.

like image 161
marcvangend Avatar answered Sep 27 '22 22:09

marcvangend