Drupal core 7.8
I am writing a new module and I am trying to pass form information into a .js file and have it load as the setting for a jQuery plugin that I found.
Here is what I have so far in my module:
$script = '$j("#rotation").galleryView({ panel_width: ' . variable_get('rotation_main_width', '700') . ' });';
drupal_add_js(drupal_get_path('module', 'rotation_main') .'/js/rotation_main.js', 'file');
drupal_add_js(array('rotation_main' => array('script_js' => $script)), 'setting');
Here is what I have in the rotation_main.js file:
var $j = jQuery.noConflict();
(function ($j) {
$j(document).ready(function(){
Drupal.settings.rotation_main.script_js;
});
}(jQuery));
My page loads and the above rotation_main.js just loads as is when I really want it to load like the following:
var $j = jQuery.noConflict();
(function ($j) {
$j(document).ready(function(){
$j("#rotation").galleryView({
panel_width: 700
});.
});
}(jQuery));
I need to have the Drupal.settings.rotation_main.script_js variable show up in the javascirpt file as $j("#rotation").galleryView({ panel_width: 700 });.
When I go the page after everything is loaded and put in the Drupal.settings.rotation_main.script_js into firebug it does display the proper info back as a string.
I want the user to be able to enter in different widths for the panel_width in the javascript so they have more control over how big the roation is.
Please help I have been looking into this for ever. I am so stuck but I feel so close.
Comments
Read documentations of
Read documentations of 'drupal_add_js'. You have to pass the variables to js using this function. For example:
---
Drupalika | دروپال فارسی
I think that is what I am
I think that is what I am doing in the this line from above.
Module Name: rotation_main
Key: script_js
Value: $script
Any more help you can offer is much appreciated.
I got it to work... Not Sure if it is most elegant but it works
I actually figured out that Drupal extends jquery at the very end of the head tag. I was calling the Drupal.settings.rotation_main.script_js; in my .js file just before that so it didn't exist yet. There for nothing was working. Once I switched the drupal_add_js to load in the footer as well as placed it into a and placed into "function rotation_main_page_build(){" function (Code below) and updated the rotation_main.js below to print it to the page after the variable is available it worked great.
Updated Drupal_add_js and placed into "function rotation_main_page_build(){" function:
Updated rotation_main.js:
Hope this helps. Oh and if there is a more elegant way to do this please help I am all hears.
Thanks,
Joe
Drupalizing it, and cleaning
Drupalizing it, and cleaning it up a little:
PHP:
JS:
Contact me to contract me for D7 -> D10/11 migrations.
I am trying to Drupalizing
I am trying to Drupalizing it. Here is what I have and I keep running into an error with my roation_main.js file.
rotation_main.module file snippet
rotation_main.js code
The .js file doesn't load. Am I doing something wrong with the .js file.
I am getting an error in firefox something about "missing : after property id" and it is referencing "$j("#rotation").galleryView({".
Thanks so much for you help.
Drupal.behaviors is the
Drupal.behaviors is the Drupalized version of $(document).ready(). You are using both. On top of this, you are setting noconflict, which you don't need to do.
Here is the entire framework of a Drupal js document:
Nothing goes before the above code, nothing goes after it. You can remove the functions f1() and f2() if you don't have any functions to be used in this script. You don't need to call jQuery.noConflict(). You don't need to call $(document).ready(). Just use the above as your framework. You don't need to use $j, as using $ alone is entirely fine inside this framework.
Contact me to contract me for D7 -> D10/11 migrations.
Here is an example of a js
Here is an example of a js script that would work in Drupal.
PHP:
my_module.js:
In the above javascript, Drupal.behaviors.myModule is executed on page load, which calls the function changeBackground(), which in turn sets the body background color to the color that was passed from the PHP.
Contact me to contract me for D7 -> D10/11 migrations.