Nothing confuses me faster than mixing php and js (and especially escaping the quotes). In this case, my confusion is the concept and not the gobblygook. I have a module invoking hook_block, and a function that creates and returns the block content. In this function I invoke drupal_add_js to gather the contents of ./mymodule.js So far so good. However, the js needs to be dynamic...there are two function values that need to be embedded in it. I'm thinking that with this being the case, drupal_add_js might not be the way to go (back to escaping quotes), but wanted to poll first for best practice, since I'll be contributing this module. Thanks, Jeff
So far so good. However, the js needs to be dynamic...there are two function values that need to be embedded in it. I'm thinking that with this being the case, drupal_add_js might not be the way to go (back to escaping quotes), but wanted to poll first for best practice, since I'll be contributing this module.
See http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js... in particular: 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.
Hi Fred. I -have- read that, but I guess when I read the description of inline referring to the current page... I have to force myself to think of a function creating block contents for a function that creates a block for a module AS being the current page as opposed to being busy -creating- the current page! With regards to the dynamic values. If I were putting the js directly in the module code rather than a .js file, I would do something like: print "var x='$myvar'"; What's the normal way of handling this when importing a .js? Would I put a placeholder in the js and use regex to plug in the values? On 11/28/2010 10:45 AM, Fred Jones wrote:
See http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js... in particular:
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.
Jeff You still want to drupal_add_js(), but instead of one call you want two. The first is to add your mymodule.js The second is to do something like $settings = array( 'MyModule' => array( 'value1' => 'some value', 'value2' => 'some other value' ) ); drupal_add_js( $settings, 'setting' ); You also need to modify mymodule.js to use the values something like var settings = Drupal.settings.MyModule; // Now you can use // settings.value1 and settings.value2 (of course you need to change MyModule to your actual module name and I would suggest more meaningful names than value1 and value2) Nevets On 11/28/2010 9:43 AM, jeff@ayendesigns.com wrote:
Nothing confuses me faster than mixing php and js (and especially escaping the quotes). In this case, my confusion is the concept and not the gobblygook.
I have a module invoking hook_block, and a function that creates and returns the block content. In this function I invoke drupal_add_js to gather the contents of ./mymodule.js
So far so good. However, the js needs to be dynamic...there are two function values that need to be embedded in it. I'm thinking that with this being the case, drupal_add_js might not be the way to go (back to escaping quotes), but wanted to poll first for best practice, since I'll be contributing this module.
Thanks,
Jeff
Ah, gotcha. Not sure this is more intuitive than the other way, but if it's common practice then so be it :) Thanks! On 11/28/2010 11:02 AM, Steve Ringwood wrote:
Jeff
You still want to drupal_add_js(), but instead of one call you want two.
The first is to add your mymodule.js
The second is to do something like
$settings = array( 'MyModule' => array( 'value1' => 'some value', 'value2' => 'some other value' ) );
drupal_add_js( $settings, 'setting' );
You also need to modify mymodule.js to use the values something like
var settings = Drupal.settings.MyModule; // Now you can use // settings.value1 and settings.value2
(of course you need to change MyModule to your actual module name and I would suggest more meaningful names than value1 and value2)
Nevets
On 11/28/2010 9:43 AM, jeff@ayendesigns.com wrote:
Nothing confuses me faster than mixing php and js (and especially escaping the quotes). In this case, my confusion is the concept and not the gobblygook.
I have a module invoking hook_block, and a function that creates and returns the block content. In this function I invoke drupal_add_js to gather the contents of ./mymodule.js
So far so good. However, the js needs to be dynamic...there are two function values that need to be embedded in it. I'm thinking that with this being the case, drupal_add_js might not be the way to go (back to escaping quotes), but wanted to poll first for best practice, since I'll be contributing this module.
Thanks,
Jeff
Jeff Both this approach (settings) and inline can work. I suggested this because you have a script that needs dynamic values. Making the script a function you can call with the two value and the inline approach would be another approach. Nevets On 11/28/2010 10:04 AM, jeff@ayendesigns.com wrote:
Ah, gotcha. Not sure this is more intuitive than the other way, but if it's common practice then so be it :) Thanks!
On 11/28/2010 11:02 AM, Steve Ringwood wrote:
Jeff
You still want to drupal_add_js(), but instead of one call you want two.
The first is to add your mymodule.js
The second is to do something like
$settings = array( 'MyModule' => array( 'value1' => 'some value', 'value2' => 'some other value' ) );
drupal_add_js( $settings, 'setting' );
You also need to modify mymodule.js to use the values something like
var settings = Drupal.settings.MyModule; // Now you can use // settings.value1 and settings.value2
(of course you need to change MyModule to your actual module name and I would suggest more meaningful names than value1 and value2)
Nevets
On 11/28/2010 9:43 AM, jeff@ayendesigns.com wrote:
Nothing confuses me faster than mixing php and js (and especially escaping the quotes). In this case, my confusion is the concept and not the gobblygook.
I have a module invoking hook_block, and a function that creates and returns the block content. In this function I invoke drupal_add_js to gather the contents of ./mymodule.js
So far so good. However, the js needs to be dynamic...there are two function values that need to be embedded in it. I'm thinking that with this being the case, drupal_add_js might not be the way to go (back to escaping quotes), but wanted to poll first for best practice, since I'll be contributing this module.
Thanks,
Jeff
Nevets, Understood and you're correct. I was just musing about the settings structure instead of calling regex, but as the former has been put in place to avoid needing to do the latter, it's easier even if not more intuitive! Thanks again.
Check out this handbook page for passing parameters to javascript: http://drupal.org/node/127067 <http://drupal.org/node/127067>Victor Kane http://awebfactory.com.ar http://projectflowandtracker.com On Sun, Nov 28, 2010 at 1:14 PM, <jeff@ayendesigns.com> wrote:
Nevets,
Understood and you're correct. I was just musing about the settings structure instead of calling regex, but as the former has been put in place to avoid needing to do the latter, it's easier even if not more intuitive!
Thanks again.
Thanks Victor. That link had another link in the comments which shows a good example of how to use settings! On 11/28/2010 11:18 AM, Victor Kane wrote:
Check out this handbook page for passing parameters to javascript:
Victor Kane http://awebfactory.com.ar http://projectflowandtracker.com
I'm close...but not quite there. I look at the page source, and the reference to my js file is there, as is a CDATA block listing all the settings contents (don't know if that -should- be there, but it's handy), which shows that what I passed in the settings array is there. I also have the block being inserted into the page. What is not there is the content of the .js file. I've tried the two drupal_add_js calls in both possible order. Here's what I have: mymodule function mymodule_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $block[$delta] = array( 'info' => t('Mymodule Block'), 'cache' => FALSE, ); return $block; break; case 'view': $block['subject'] = "Mymodule Block"; $block['content'] = _make_block(); return $block; break; } } function _make_block() { if (isset($_SESSION['setmyblock'])) { drupal_set_message('setting'); $settings = array( 'mymodule' => array( 'name' => $_SESSION['mymodule_name'], 'total' => $_SESSION['mymodule_total'] ) ); drupal_add_js($settings, 'setting'); drupal_add_js(drupal_get_path('module', 'mymodule') .'/mymodule.js', 'module', 'header', FALSE, FALSE); $block = 'test'; } return $block; } mymodule.js <script type="text/javascript" src="http://www.mydomain.com/test.js"></script> <script type="text/javascript"> var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); </script>
On 11/28/2010 9:17 AM, jeff@ayendesigns.com wrote:
<script type="text/javascript" src="http://www.mydomain.com/test.js"></script> <script type="text/javascript"> var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); </script>
The problem is likely order of code running. Settings won't have been read in yet. You need to use jquery's s ready() function. IMO you should put this directly into your test.js file and not embed inline javascript at all. If your javascript is going to do a .write() or something, you are best off using a <div> that will be replaced. So at the bottom of your test.js, something like this: // $(function() { }) is a synonym for $.ready(). // This ensures that the code is not executed until the page is rendered. $(function() { var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); });
I realize now that I don't know whether I'm coming or going... Excellent the distinction between server side and client side, would be good to do a little drawing showing the order in which things get executed and the params passed (interaction diagram or sequence diagram). Victor On Sun, Nov 28, 2010 at 4:00 PM, Earl Miles <merlin@logrus.com> wrote:
On 11/28/2010 9:17 AM, jeff@ayendesigns.com wrote:
<script type="text/javascript" src="http://www.mydomain.com/test.js"></script> <script type="text/javascript"> var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); </script>
The problem is likely order of code running. Settings won't have been read in yet. You need to use jquery's s ready() function. IMO you should put this directly into your test.js file and not embed inline javascript at all. If your javascript is going to do a .write() or something, you are best off using a <div> that will be replaced. So at the bottom of your test.js, something like this:
// $(function() { }) is a synonym for $.ready(). // This ensures that the code is not executed until the page is rendered. $(function() { var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); });
I realize now that I don't know whether I'm coming or going... Excellent the distinction between server side and client side, would be good to do a little drawing showing the order in which things get executed and the params passed (interaction diagram or sequence diagram). Victor On Sun, Nov 28, 2010 at 4:00 PM, Earl Miles <merlin@logrus.com> wrote:
On 11/28/2010 9:17 AM, jeff@ayendesigns.com wrote:
<script type="text/javascript" src="http://www.mydomain.com/test.js"></script> <script type="text/javascript"> var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); </script>
The problem is likely order of code running. Settings won't have been read in yet. You need to use jquery's s ready() function. IMO you should put this directly into your test.js file and not embed inline javascript at all. If your javascript is going to do a .write() or something, you are best off using a <div> that will be replaced. So at the bottom of your test.js, something like this:
// $(function() { }) is a synonym for $.ready(). // This ensures that the code is not executed until the page is rendered. $(function() { var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); });
'Coming' or 'Going' would depend on your vantage point during the observation, the timing of your observation, and whether you were making a direct observation or wrapping your observation in another perspective on behalf of the another. I agree with the value of the diagram, especially if its shown in conjunction with the timing of Drupal's events On 11/28/2010 02:47 PM, Victor Kane wrote:
I realize now that I don't know whether I'm coming or going...
Excellent the distinction between server side and client side, would be good to do a little drawing showing the order in which things get executed and the params passed (interaction diagram or sequence diagram).
Victor
Makes sense (timing...the 4th dimension how it all is non-intuitive). So, my Drupal function is now function _make_block() { if (isset($_SESSION['test'])) { drupal_set_message('setting'); $settings = array( 'mymodule' => array( 'name' => $_SESSION['mymodule_name'], 'total' => $_SESSION['mymodule_total'] ) ); drupal_add_js($settings, 'setting'); drupal_add_js(drupal_get_path('module', 'mymodule) .'/mymodule.js', 'module', 'header', FALSE, FALSE); $block = 'test'; } return $block; } Not sure whether the second drupal_add_js call should be of type module or inline at this point. The contents of my js file are //<script type="text/javascript" src="http://www.mydomain.com/mystuff.js"></script> $(function() { var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); }); I commented out the reference to the other js file within my .js file for now. I guess I'll need to handle it with another drupal_add_js call. However, I'm still getting only <script type="text/javascript" src="/sites/all/modules/_custom/mymodule/mymodule.js?1290973249"></script> but nothing that's in it appears on the page. I simply get the divs for the test block and the word 'test' as its content. On 11/28/2010 02:00 PM, Earl Miles wrote:
The problem is likely order of code running. Settings won't have been read in yet. You need to use jquery's s ready() function. IMO you should put this directly into your test.js file and not embed inline javascript at all. If your javascript is going to do a .write() or something, you are best off using a<div> that will be replaced. So at the bottom of your test.js, something like this:
// $(function() { }) is a synonym for $.ready(). // This ensures that the code is not executed until the page is rendered. $(function() { var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); });
On Sunday, November 28, 2010 1:53:01 pm jeff@ayendesigns.com wrote:
Makes sense (timing...the 4th dimension how it all is non-intuitive). So, my Drupal function is now
function _make_block() { if (isset($_SESSION['test'])) { drupal_set_message('setting'); $settings = array( 'mymodule' => array( 'name' => $_SESSION['mymodule_name'], 'total' => $_SESSION['mymodule_total'] ) ); drupal_add_js($settings, 'setting'); drupal_add_js(drupal_get_path('module', 'mymodule) .'/mymodule.js', 'module', 'header', FALSE, FALSE); $block = 'test'; } return $block; }
This part looks correct, I think. Basically, I consider the "inline" property of drupal_add_js() to be almost a bug, not a feature. Don't use it if you can possibly avoid it.
Not sure whether the second drupal_add_js call should be of type module or inline at this point. The contents of my js file are
'module' is correct, because you're adding a .js file from a module.
//<script type="text/javascript" src="http://www.mydomain.com/mystuff.js"></script> $(function() { var settings = Drupal.settings.mymodule; myfunction(settings.name,settings.total); });
You don't want that first line at all. That's only if you're putting it in the HTML directly. If it's just in a JS file you do not want it.
I commented out the reference to the other js file within my .js file for now. I guess I'll need to handle it with another drupal_add_js call.
Correct. <script> tags don't work within JS files. They're an HTML thing.
However, I'm still getting only
<script type="text/javascript" src="/sites/all/modules/_custom/mymodule/mymodule.js?1290973249"></script>
That's all drupal_add_js() will give you in the page itself. That's good.
but nothing that's in it appears on the page. I simply get the divs for the test block and the word 'test' as its content.
Sounds like it's time to fire up Firebug and see where the Javascript is failing. --Larry Garfield
Yup, sure was. Turns out I had tossed in a typo at some point. I also found, regarding the commented line to the external .js file, that you can't use drupal_add_js for that ... but drupal_set_html_head() does the trick. Thanks all! On 11/28/2010 04:22 PM, Larry Garfield wrote:
Sounds like it's time to fire up Firebug and see where the Javascript is failing.
--Larry Garfield
On 11/28/2010 8:14 AM, jeff@ayendesigns.com wrote:
Nevets,
Understood and you're correct. I was just musing about the settings structure instead of calling regex, but as the former has been put in place to avoid needing to do the latter, it's easier even if not more intuitive!
I'm not sure there's any such thing as intuitive when you're talking about 1 language running on one (or more servers) trying to then run code running on the client (i.e, the browser) and then passing data to it.
Ha! Definitely. This can be shown by watching someone's eyes while trying to explain why js cannot control php values and, once the page is rendered, vice-versa. They typically glaze over after 30 seconds. On 11/28/2010 11:54 AM, Earl Miles wrote:
I'm not sure there's any such thing as intuitive when you're talking about 1 language running on one (or more servers) trying to then run code running on the client (i.e, the browser) and then passing data to it.
participants (6)
-
Earl Miles -
Fred Jones -
jeff@ayendesigns.com -
Larry Garfield -
Steve Ringwood -
Victor Kane