views:

31

answers:

2

I'm trying to integrate a javascript library for drag&drop on tables into one page of my custom Drupal module. I've included the js file using drupal_add_js, but I don't know how to initialize it.

The documentation for that library states that an init function should be called like

<body onload="REDIPS.drag.init()">

How would I do that in Drupal? Or has Drupal some better way of initializing the script?

A: 

You could try adding another drupal_add_js call in the same function as your other add_js:

drupal_add_js('REDIPS.drag.init();','inline','header',true);

The last param "true" is to defer the execution of the script. I hope that helps in some way!

PIM
+3  A: 

Drupal has its own mechanism for this, involving adding a property to Drupal.behaviors. See this page: http://drupal.org/node/205296

Drupal.behaviors.redipsDragBehavior = function() {
    REDIPS.drag.init();
};

From the linked page:

Any function defined as a property of Drupal.behaviors will get called when the DOM has loaded.

Tim Down
Thanks. I suspected there was a Drupal way of doing it but I couldn't find it.
Fabian
Didn't know about the Drupal javascript stuff. This is great to know.
Jergason
If REDIPS.drag.init() doesn't, you should add your own check to avoid multiple initlization. Behaviors function are called each time a element is added to the page through JavaScript and not only on page load. Something like Drupal.behaviors.redipsDragBehavior = function() { if(!argument.callee.init) { REDIPS.drag.init(); argument.callee.init = TRUE;}};
mongolito404