[development] php and js in a programmatic block

Earl Miles merlin at logrus.com
Sun Nov 28 19:00:59 UTC 2010


On 11/28/2010 9:17 AM, jeff at 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);
});




More information about the development mailing list