When drupal_add_js() is called in the module_init() function, will the loaded JS library have global presence with regards to access to the library by other modules?
Example:
Let’s say that there are two modules – each requires one JS library, and both JS libraries are identically named. The JS libraries exist in their respective module directories. Though both JS libraries are identically named, their contents differ such that each module cannot use the other module’s JS library – doing so will cause the module to fail.
A problem will thus arise IF in fact JS libraries are loaded with global presence following the above-stated method. When the first module is loaded, its JS library will be applied globally across all modules. Next, the second module is loaded and its JS library will be applied globally across all modules. At this stage, both modules are active. Since the JS libraries are identically named, the second module’s JS library would effectively replace the first module’s JS library thus causing the first module to fail.
Thanks!
The JavaScript code that a module adds with
drupal_add_js()is added to the page, and the list of the JavaScript code added to a page is one, and global for every module. This means that the JavaScript code added from a module is visible to every module.In fact, that list is contained in a static variable used by
drupal_add_js(), and the JavaScript code is added to the page in the page.tpl.php file using the following code:$scriptsis initialized in template_preprocess_page() with the following code, which returns a string containing the HTML to use for the<script>tags basing on the content of that static variable.If for example, two modules calls the function with
drupal_add_js(drupal_get_path('module', 'first_module') . '/jquery_plugin_tree'), anddrupal_add_js(drupal_get_path('module', 'second_module') . '/jquery_plugin_tree'), then the page will contain two<script>tags that point to two files, even when the content of the file is the same in both the cases.In the case two modules use the same path for the JavaScript file, then there will be just a
<script>tag added.