Posted By

kkaefer on 08/22/06


Tagged

drupal documentation


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

panatlantica
btully


Docs for drupal_add_js()


 / Published in: HTML
 

  1. In the page header:
  2. 26. Vastly extended <code>drupal_add_js()</code>
  3.  
  4.  
  5.  
  6. Below:
  7. <h2>Vastly extended <code>drupal_add_js()</code></h2>
  8. <p>The function to add JavaScript to a Drupal page has been reworked:</p>
  9.  
  10. <h3>Definition</h3>
  11. <p><small>drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE)</small><br />
  12. <a href="http://api.drupal.org/api/HEAD/file/includes/common.inc">includes/common.inc</a>, line 1329</p>
  13.  
  14. <h3>Description</h3>
  15. <p>Add a JavaScript file, setting or inline code to the page.</p>
  16. <p>The behavior of this function depends on the parameters it is called with. Generally, it handles the addition of JavaScript to the page, either as reference to an existing file or as inline code. The following actions can be performed using this function:</p>
  17.  
  18. <ul>
  19. <li>Add a file ('core', 'module' and 'theme'): Adds a reference to a JavaScript file to the page. JavaScript files are placed in a certain order, from 'core' first, to 'module' and finally 'theme' so that files, that are added later, can override previously added files with ease.</li>
  20. <li>Add inline JavaScript code ('inline'): Executes a piece of JavaScript code on the current page by placing the code directly in the page. This can, for example, be useful to tell the user that a new message arrived, by opening a pop up, alert box etc.</li>
  21. <li>Add settings ('setting'): Adds a setting to Drupal's global storage of JavaScript settings. Per-page settings are required by some modules to function properly. The settings will be accessible at Drupal.settings.</li>
  22. </ul>
  23.  
  24. <h3>Parameters</h3>
  25. <p>$data (optional) If given, the value depends on the $type parameter:</p>
  26. <ul>
  27. <li>'core', 'module' or 'theme': Path to the file relative to <a href="http://api.drupal.org/api/HEAD/function/base_path" title="Returns the base URL path of the Drupal installation. At the very least, this will always default to /." class="local">base_path</a>().</li>
  28. <li>'inline': The JavaScript code that should be placed in the given scope.</li>
  29. <li>'setting': An array with configuration options as associative array. The array is directly placed in Drupal.settings. You might want to wrap your actual configuration settings in another variable to prevent the pollution of the Drupal.settings namespace.</li>
  30. </ul>$type (optional) The type of JavaScript that should be added to the page. Allowed values are 'core', 'module', 'theme', 'inline' and 'setting'. You can, however, specify any value. It is treated as a reference to a JavaScript file. Defaults to 'module'.
  31. <p>$scope (optional) The location in which you want to place the script. Possible values are 'header' and 'footer' by default. If your theme implements different locations, however, you can also use these.</p>
  32. <p>$defer (optional) If set to TRUE, the defer attribute is set on the <script> tag. Defaults to FALSE. This parameter is not used with $type == 'setting'.</p>
  33. <p>$cache (optional) If set to FALSE, the JavaScript file is loaded anew on every page call, that means, it is not cached. Defaults to TRUE. Used only when $type references a JavaScript file.</p>
  34.  
  35. <h3>Return value</h3>
  36. <p>If the first parameter is NULL, the JavaScript array that has been built so far for $scope is returned.</p>
  37.  
  38.  
  39.  
  40. In the page header:
  41. 27. Removed <code>drupal_call_js()</code>
  42.  
  43.  
  44. Below:
  45. <h3>Removed <code> drupal_call_js()</code></h3>
  46. <p>As it is now very easy to attach JavaScript to a page, this function is not needed anymore. Use this syntax instead:</p>
  47. <?php
  48. drupal_add_js('myCustomFunction(your, parameters, here)', 'inline');
  49. ?>
  50. <p>If you have more complex parameters that should be passed to the function, consider using <code>drupal_to_js()</code> to convert them to a JSON object before.</p>
  51. <p><strong>Note:</strong> It is recommended to not call functions in the page header directly. Instead use unobtrusive JavaScript that automatically runs when the page is loaded.</p>

Report this snippet  

You need to login to post a comment.