Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I've made a cost calculator script and want to use it on my Drupal 7 site.

I heard to use drupal_add_js, but I don't know in what context.

share|improve this question

3 Answers

A better approach would be to get the path from Drupal, and you have to put the code in the form function;

function mymodule_form($form, &$form_state) {

... some of your forms goes here

    $form['#attached']['js'][] = drupal_get_path('module', 'mymodule') . '/myscript.js';

  return $form;

Also add your javascript between the code below

(function($)  {
  function mymodule_function(context)  {

  // from here

  // paste your code in here

  // till here

  }

  Drupal.behaviors.custommodule = {
    attach: function(context)  {
      mymodule_function(context);
    }
  }
})(jQuery);
share|improve this answer
Where would I put this? – user16277 Apr 8 at 0:07
please check this link drupal.org/node/121997 – user1973842 Apr 11 at 17:54

if your script is saved as .js you can add it like that in html.tpl.php:

<?php drupal_add_js('location/of/my/script/my_script.js'); ?>

more info here

OR

<script src="location/of/my/script/my_script.js"></script> 

in your block or node body (make sure that you select format that does not filter JavaScript i.e. "PHP code")

share|improve this answer
Do I insert this in the node edit form? Is it fine to put the .js file in the sites folder? – user16277 Apr 8 at 0:10
yes - you can put it in node edit form if you wish. just remember to enable in "PHP filter" in modules' core section and set "text format" to "PHP code" in node edit form; and yes - you can put .js in sites folder. – sly Apr 8 at 10:09
Followed your instructions and this did not work for me. – user16277 Apr 8 at 16:38
did you clear cache? another solution which I do not recommend but it probably will work is rather than using drupal_add_js is simply [link]<script src="location/of/my/script/my_script.js"></script> – sly Apr 9 at 11:46
I need to correct myself: By the time Drupal reaches the block data, the contents of <head> have already been generated, this means that calling drupal_add_js through the block's body field will have no effect. Please use <script> as described above or put drupal_add_js in html.tpl.php of your theme – sly Apr 9 at 11:58
show 2 more comments

Use function in tempale.php

Add JS file in block -

function MYTHEME_preprocess_block(&$variables) {  
    drupal_add_js(drupal_get_path('theme', 'MYTHEME') .'/mytheme.js', 'file');
} 

Add JS file in page or node -

function MYTHEME_preprocess_page(&$variables) {  
     drupal_add_js(drupal_get_path('theme', 'MYTHEME') .'/mytheme.js', 'file');

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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