JavaScript libraries, basics and examples for Drupal 7. JavaScript is provided using the Library API.
JavaScript libraries and plugins
Following plugins are provided by Drupal 7:- jQuery 1.4.4
- jQuery UI 1.8.6
- jQuery Cookie - read, write and delete cookies
- jQuery Form - AJAX forms
- jQuery Once - filters out all elements that had the same filter applied to them previously
- jQuery BBQ - back button and query library
- Farbtastic - color wheel
Using jQuery
Drupal uses no conflict mode. This means that the $ variable is relinquished so Drupal can work with other JavaScript libraries that may use the $ variable. More detail is available at https://api.jquery.com/jQuery.noConflict.Wrap your code in an anonymous function and choose an alias, this way you can use $ as jQuery variable:
Source code viewer
(function($) { //... });Programming Language: jQuery
Using jQuerys ready in your module. This is triggered when all assets such as images have been completely received.
Source code viewer
drupal_add_js('(function($){$().ready(function(){ //... });})(jQuery);', 'inline');Programming Language: PHP
Adding external JavaScript file
Example that shows you how to add external JavaScript libraries in Drupal.Source code viewer
drupal_add_js('http://ajax.googleapis.com/ajax/libs/webfont/1.4.8/webfont.js', 'external');Programming Language: PHP
Pass variables from PHP to JavaScript in Drupal
Example that shows you how to pass variables from PHP to JavaScript in Drupal. Passing variables from PHP. You can pass strings, arrays and objects.Use of variables in JavaScript.Source code viewer
Programming Language: PHP
Source code viewer
console.log(Drupal.settings.my_variable);Programming Language: Javascript
Add script files by specifying it in the .info file
Source code viewer
scripts[] = javascript.jsProgramming Language: YAML
JS in Form API
Source code viewer
$form['#attached']['js'][] = $module_path . '/js/chapter_edit_position.js'; 'chapter_edit_position' => $form['nid']['#value'], ), 'type' => 'setting', );Programming Language: PHP
Scripts from Drupal 7 core
Source code viewer
drupal_add_library('system', 'drupal.ajax'); drupal_add_library('system', 'drupal.batch'); drupal_add_library('system', 'drupal.progress'); drupal_add_library('system', 'drupal.form'); drupal_add_library('system', 'drupal.states'); drupal_add_library('system', 'drupal.collapse'); drupal_add_library('system', 'drupal.textarea'); drupal_add_library('system', 'drupal.autocomplete'); drupal_add_library('system', 'drupal.vertical-tabs'); drupal_add_library('system', 'ui'); drupal_add_library('system', 'ui.datepicker'); drupal_add_library('system', 'ui.dialog'); drupal_add_library('system', 'ui.draggable'); drupal_add_library('system', 'ui.droppable'); drupal_add_library('system', 'ui.mouse'); drupal_add_library('system', 'ui.position'); drupal_add_library('system', 'ui.progressbar'); drupal_add_library('system', 'ui.resizable'); drupal_add_library('system', 'ui.selectable'); drupal_add_library('system', 'ui.accordion'); drupal_add_library('system', 'ui.slider'); drupal_add_library('system', 'ui.sortable'); drupal_add_library('system', 'ui.autocomplete'); drupal_add_library('system', 'ui.tabs'); drupal_add_library('system', 'ui.button'); drupal_add_library('system', 'ui.widget'); drupal_add_library('system', 'jquery.cookie'); drupal_add_library('system', 'jquery.bbq'); drupal_add_library('system', 'jquery.form'); drupal_add_library('system', 'jquery.once');Programming Language: PHP