The "Drupal.behaviors" doesn't behave as "document.ready", if I use a document.ready I will have the correct map but if I do a Drupal.behaviors, there will be an error related to the openlayers library function which is not found :(

After check, the OpenLayers.js is loaded after my module js. Is it possible to make it load after another one ? I though adding a dependency on another module would make him load before the other one.

If I use a setTimeout it's working but it's not the best solution !

Do you have any idea how I can do that ?

Thank you guys

(function ($) {
Drupal.OL_lat_long = Drupal.OL_lat_long || {};
    Drupal.behaviors.OL_lat_long = {
         attach: function (context, settings) {
                    var map = $('#openlayers-map').data('openlayers').openlayers;
                    var pointLayer= map.layers[1];
                    proj4326 = new OpenLayers.Projection("EPSG:4326");
                    proj900913 = new OpenLayers.Projection("EPSG:900913");

        //  });

2 Answers 2


Maybe you could try altering the weight of the script you add in your module?

drupal_add_js(drupal_get_path('module', 'MYMODULE') .'/myscript.js', array(
    'weight' => '999',
  • I added it in the info file, is there a way to do it to the info file instead ?
    – GwenM
    Jul 30, 2013 at 12:47
  • I don't think you can set weight in an .info file for a script. You can either use drupal_add_js in a module or you implement hook_js_alter and change weight on an already added script. But why go around the problem :) Jul 30, 2013 at 12:53
  • yeah sure, but I was just wondering if it was possible to do it on the info file :) Thank you anyway !
    – GwenM
    Jul 30, 2013 at 13:06

I had a similar scenario where I needed to track site wide events through mixpanel on events like submitting a form (which will reload the page and in some times will redirect to some other page and so the minimum 300 ms time needed for mixpanel tracking was failing).

I followed this approach :

I added a cookie inside a Drupal behavior while some required event occurs using -

set_cookie('cookie_name', SOME_VALUE); 

Then in the module file under proper hook like say 'hook_node_view', 'hook_user_view', 'hook_user_login', 'hook_preprocess_page', 'hook_user_insert' or so, I added code like below :

if (isset($_COOKIE['cookie_name'])) {// Validation
  $value = 0;// Variable Declaration 
  $value = drupal_json_decode( $_COOKIE ['cookie_name'] );// Get value From Cookie

  if (isset($value ['via_btn'])) {// Validation
    if (!empty($value ['via_btn'])) {// Validation            
      drupal_add_js(array('YOUR_MODULE_NAME' => array('SOME_NAME' => 1)), 'setting');

  // Delete Cookie
  setcookie("cookie_name", '', time() - 3600);
  if (isset($_COOKIE["cookie_name"])) {
    setcookie("cookie_name", '', 1, '/');

Kept the below code in theme.js (so that it's available in all pages) :

function set_cookie(cookie_name, cookie_val) {
  var expires = "";
  if(cookie_name && cookie_val){
    document.cookie = cookie_name + "=" + JSON.stringify({
    })+ expires + "; path=/";
    document.cookie = cookie_name + "=" + "" + expires + "; path=/";
  return 1;

Now added the below code in the js file :

Drupal.behaviors.SOME_SUITABLE_NAME = {
  attach: function (context, settings) {
    if(typeof Drupal.settings.YOUR_MODULE_NAME !== 'undefined' && typeof msg === 'undefined') {
      msg = Drupal.settings.YOUR_MODULE_NAME.SOME_NAME; 
      if (msg !== undefined && msg === 1) {
    // Do Something

So, here, what I am doing is trying to track an event after going through some proper hook so that I can skip using 'setTimeout'.

Note : In my case, I have used

if it worked. I went through the above approach only when default drupal behavior approach or 'document.ready' didn't work.

Hope it helps

  • Note that Drupal already has a cookie library in core so that you can use code like $.cookie('my_cookie'). You will need to add the system library yourself though where you need it. orga.cat/posts/jquerycookie-drupal-7
    – doublejosh
    May 20, 2014 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.