i try to use drupal_add_js() function to add some java script & jquery library for my module
all is ok but when i logged in admin area it show

$("#toolbar", context).once is not a function
$('#toolbar', context).once('toolbar', Drupal.toolbar.init); 

who know about this error ? Please show me how to fix it ?

Many thanks

Best regards

Hle

Comments

jaypan’s picture

.once() is not a jQuery function, so it doesn't work.

Contact me to contract me for D7 -> D10/11 migrations.

nhocmuonlanhoc’s picture

Thanks Jay Matwichuk,

but i i think it is a function of drupal core
$('#toolbar', context).once('toolbar', Drupal.toolbar.init);

it's not my function

nevets’s picture

Where does the jQuery code from? I would expect if that is Drupal related there would be a Drupal module that handles adding the javascript.

fusepilot’s picture

This is happening for me too. This is the guilty code from toolbar.js in the Toolbar module. 7.x-rc4.

// Set the initial state of the toolbar.
$('#toolbar', context).once('toolbar', Drupal.toolbar.init);

// Toggling toolbar drawer.
$('#toolbar a.toggle', context).once('toolbar-toggle').click(function(e) {
  Drupal.toolbar.toggle();
  // Allow resize event handlers to recalculate sizes/positions.
  $(window).triggerHandler('resize');
  return false;
});
ethiaa’s picture

Subscribing

WorldFallz’s picture

don't know why it's not working, but it was definitely added to core with d7:

http://drupal.org/node/224333#jquery_once

avo_liao’s picture

Subscribe.
I have the same error when using drupal_add_js()

tirdadc’s picture

Same issue, had this appear out of nowhere.

WilliamB’s picture

subscribe

pthite’s picture

ditto.

pthite’s picture

My problem looks like it was coming from the jQuery Update module. Turned that off and things seem to be working again.

markbrule’s picture

I had the same error reported in my Firebug console, but the real problem was that I had an error in the HTML I was creating. That caused the js files to not load properly. I turned off the toolbar module, and got an error that I traced back to the typo (missed a quote). Fixed that typo, re-enabled the toolbar module, and everything is good.

hye_98’s picture

In my case I found the root issue is that current Drupal 7 core is built on jQuery 1.4, and Once plugin seems only have a version compabible upto jQuery 1.4.x. So this problem occurs when I load jQuery 1.7.2. Unfortunately I have a widget plugin which requires higher version. In addition, Drupal.js is using jQuery 1.4, and I have to call my widget within behavior. I am using an ugly solution now, have two version of jQuery, 1.4 and 1.7.2 on same page, using the $.noConflict method of jQuery. If anyone have a better solution upgrading jQuery, I want to learn it too.

manmanwahaha’s picture

subscribe

joshuacoombe’s picture

I had this problem today and have since rectified it.

This error happens when the Drupal core tries to run the jQuery function once() and it isn't found. Once() is not a native jQuery function but expanded into jQuery by Drupal in the client.

Diagnosis?
The problem happens in one of the following circumstances:

  1. The once() function was never added to the jQuery object
  2. The once() function was added to the jQuery object which was destroyed
  3. Something else?

Fixes

The first case may arise if you've hacked the core. Or if you are diliberately not including javascript files that drupal would otherwise include.

The second case may arise if you are re-initialising the jQuery object. This is the mistake I was making earlier today. I was doing this:
drupal_add_js('http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js', array('every_page'=>FALSE));

to upgrade my version of jQuery to 1.8.0 so the libraries I was using would work. Don't do that, use the jQuery update module.

If the third case arises... you're in real trouble.

I hope this helps.

muthuraman393’s picture

Ya same issue here :(

Strycker’s picture

subscribe.

P.S. tried to solve problem with this
https://drupal.org/node/1793366

still have

jaypan’s picture

This is probably happening because you are calling $.once() before it has been loaded.

$.once() should not be called before Drupal has loaded. If you are using the jQuery $(document).ready() function, you need to switch to Drupal.behaviors. Here is a skeleton you can use:

// My JavaScript file
(function($, Drupal) {
  function myCustomFunction() {
    $(".some_other_element").once("my-other-element", function() {
      // Some code that should be executed only one time
      // $(this) can be used to reference the element
      $(this).css("background-color", "#00FF00");
    });
  }

  Drupal.behaviors.myUniqueID.behaviors = {
    attach:function() {
      // Called when document is loaded
      // Safe to use $.once()
      $(".some_element").once("my-element", function() {
        // Some code that should be executed only one time
        // $(this) can be used to reference the element
        $(this).css("background-color", "#FF0000");
      });

      // Can also call a function and use $.once() inside the function
      myCustomFunction();
    }
  };
}(jQuery, Drupal));

Drupal.behaviors will actually be attached multiple times on a page, and by using $.once(), you can ensure that your code is only fired once.

Contact me to contract me for D7 -> D10/11 migrations.

xaris.tsimpouris’s picture

For me it happens when I try to add an (whatever) action within Rules, Drupal 7.24 & Rules 7.x-2.6. At this point, where this issue arises, there is no custom javascript loaded and no theme override is happening (Theme::Seven).

As an ugly hack, I found the following "solution" reading the previous posts. When I want to add a new Rules::action I disable temporarily the jQuery update module (7.x-2.3) and everything works smooth (within Rules) :(

(For crawlers) the error that consoles shows:

Error thrown at line 16, column 26206 in <anonymous function: resolveWith>(d, f) in http://stontoixo.gr/sites/all/modules/jquery_update/replace/jquery/1.5/jquery.min.js?v=1.5.2:
    throw g
called from line 16, column 134774 in v(a, c, l, n) in http://stontoixo.gr/sites/all/modules/jquery_update/replace/jquery/1.5/jquery.min.js?v=1.5.2:
    u.status=a,u.statusText=c,q?h.resolveWith(f,[t,c,u]):h.rejectWith(f,[u,c,v]),u.statusCode(j),j=b,s&&g.trigger("ajax"+(q?"Success":"Error"),[u,e,q?t:v]),i.resolveWith(f,[u,c]),s&&(g.trigger("ajaxComplete",[u,e]),--d.active||d.event.trigger("ajaxStop"))
called from line 16, column 146142 in <anonymous function: c>(e, i) in http://stontoixo.gr/sites/all/modules/jquery_update/replace/jquery/1.5/jquery.min.js?v=1.5.2:
    m&&f(j,k,m,l)

Error initially occurred at line 65, column 4 in <anonymous function: attach>(context, settings) in http://stontoixo.gr/admin/config/workflow/rules/reaction/manage/rules_redirect_on_login/add/1/action:
    $('form.user-info-from-cookie').once('user-info-from-cookie', function () {
called from line 76, column 6 in <anonymous function: Drupal.attachBehaviors>() in http://stontoixo.gr/misc/drupal.js?mxuvce:
    this.attach(context, settings);
called via Function.prototype.call() from line 16, column 22576 in <anonymous function: each>(a, c, e) in http://stontoixo.gr/sites/all/modules/jquery_update/replace/jquery/1.5/jquery.min.js?v=1.5.2:
    if(c.call(a[f],f,a[f])===!1)
called from line 74, column 2 in <anonymous function: Drupal.attachBehaviors>(context, settings) in http://stontoixo.gr/misc/drupal.js?mxuvce:
    $.each(Drupal.behaviors, function () {
called from line 542, column 6 in <anonymous function: insert>(ajax, response, status) in http://stontoixo.gr/misc/ajax.js?v=7.24:
    Drupal.attachBehaviors(new_content, settings);
called from line 400, column 6 in <anonymous function: Drupal.ajax.prototype.success>(response, status) in http://stontoixo.gr/misc/ajax.js?v=7.24:
    this.commands[response[i]['command']](this, response[i], status);
called from line 164, column 6 in <anonymous function: success>(response, status) in http://stontoixo.gr/misc/ajax.js?v=7.24:
    return ajax.success(response, status);
called via Function.prototype.apply() from line 11, column 12310 in <anonymous function: c.success>(a, b, d) in http://stontoixo.gr/sites/all/modules/jquery_update/replace/misc/jquery.form.min.js?v=2.69:
    m[f].apply(e,[a,b,d||l,l])
called via Function.prototype.apply() from line 16, column 26176 in <anonymous function: resolveWith>(d, f) in http://stontoixo.gr/sites/all/modules/jquery_update/replace/jquery/1.5/jquery.min.js?v=1.5.2:
    a.shift().apply(d,f)
called from line 16, column 134774 in v(a, c, l, n) in http://stontoixo.gr/sites/all/modules/jquery_update/replace/jquery/1.5/jquery.min.js?v=1.5.2:
    u.status=a,u.statusText=c,q?h.resolveWith(f,[t,c,u]):h.rejectWith(f,[u,c,v]),u.statusCode(j),j=b,s&&g.trigger("ajax"+(q?"Success":"Error"),[u,e,q?t:v]),i.resolveWith(f,[u,c]),s&&(g.trigger("ajaxComplete",[u,e]),--d.active||d.event.trigger("ajaxStop"))

Problem has been checked on both Chrome and Opera (Linux Mint)

--
http://1024.gr - Programming by nature

truyenle’s picture

Check your page source to make sure that you don't load multile jquery version. If you must have to do it, please use conflict elimination.