Problem/Motivation
Receiving the following errors and am unable to determine why ...
drupal.js?v=10.1.6:64 Uncaught TypeError: a(...).colorbox is not a function
at HTMLDivElement.<anonymous> (nacolorbox.js:19:11)
at Function.each (jquery.js:383:1)
at jQuery.fn.init.each (jquery.js:205:1)
at Object.attach (nacolorbox.js:6:41)
at drupal.js?v=10.1.6:166:24
at Array.forEach (<anonymous>)
at Drupal.attachBehaviors (drupal.js?v=10.1.6:162:34)
at big_pipe.js?v=10.1.6:138:10
at big_pipe.js?v=10.1.6:169:3
drupal.js?v=10.1.6:64 Uncaught TypeError: a(...).colorbox is not a function
at HTMLDivElement.<anonymous> (nacolorbox.js:19:11)
at Function.each (jquery.js:383:1)
at jQuery.fn.init.each (jquery.js:205:1)
at Object.attach (nacolorbox.js:6:41)
at drupal.js?v=10.1.6:166:24
at Array.forEach (<anonymous>)
at Drupal.attachBehaviors (drupal.js?v=10.1.6:162:34)
at drupal.init.js?v=10.1.6:32:12
at HTMLDocument.listener (drupal.init.js?v=10.1.6:20:7)
drupal.js?v=10.1.6:64 Uncaught TypeError: a(...).colorbox is not a function
at HTMLDivElement.<anonymous> (nacolorbox.js:19:11)
at Function.each (jquery.js:383:1)
at jQuery.fn.init.each (jquery.js:205:1)
at Object.attach (nacolorbox.js:6:41)
at drupal.js?v=10.1.6:166:24
at Array.forEach (<anonymous>)
at Drupal.attachBehaviors (drupal.js?v=10.1.6:162:34)
at success (ajax.js?v=10.1.6:1800:20)
at l (loadjs.min.js?v=4.2.0:1:217)
at loadjs.min.js?v=4.2.0:1:1556
There are 3 calls to the colorbox function on the page, but each error looks slightly different ... not sure if that's relevant.
My code calling it looks like ...
import jQuery from 'jquery';
(($) => {
Drupal.behaviors.naColorbox = {
attach(context) {
$('.block--collapsible', context).each((index, element) => {
const content = $(element).find('.block--collapsible__content');
let height = '92%';
try {
height = `${parseInt($(window).height() * 0.92, 10)}px`;
} catch (ex) {
// do nothing
}
$(element).colorbox({
inline: true,
href: content,
width: '92%',
height,
maxWidth: '960px',
maxHeight: '720px',
scrolling: true,
});
});
},
};
})(jQuery);
Viewing the page's source (with JS not aggregated, I see ...
<script src="/modules/contrib/colorbox/js/colorbox.js?v=10.1.6"></script>
<script src="/libraries/colorbox/jquery.colorbox-min.js?v=10.1.6"></script>
<script src="/modules/contrib/colorbox_inline/js/colorbox_inline.js?s46bqs"></script>
...
<script src="/modules/contrib/colorbox_load/js/colorbox_load.js?s46bqs"></script>
...
<script src="/themes/custom/my_theme/dist/js/00-base/06-colorbox/nacolorbox.js?s46bqs"></script>
Steps to reproduce
I just upgraded to Drupal 10.1.6 and am running Colorbox v 2.0.1
Proposed resolution
What am I missing? It seems everything is installed, but I can't make this work.
Thanks!
Comments
Comment #2
paulmckibbenThere's nothing obviously wrong based on your problem description.
I'm not sure why you're trying to invoke the colorbox method as part of attachBehaviors(). Would you want to do that in an event handler instead?
Comment #3
mahtab_alam commentedhttps://github.com/jackmoore/colorbox
Download this library and upload to web/libraries folder
Make sure the folder name should be colorbox
Comment #4
devarch commentedThis looks like an error with deprecated jquery functions in v3 and removed in v4. I have just updated some of my sites to drupal 11, and because jquery gets updated to v4, both colorbox and slick carousel are broken. The key is to update the js libraries to be JQuery compatible.
Please read the slick project page for more info about this: Slick project page
If you want to use colorbox on upgraded jquery please look at
https://github.com/jackmoore/colorbox/blob/f155cf2fa18619f9290b16d4a37aaf2e5b7c6fe1/jquery.colorbox.js
These are the modifications needed for making colorbox compatible with newer jquery versions:
https://github.com/jackmoore/colorbox/pull/908/files
Maybe a warning on the project page should be in order, just like on the slick project, to clarify that even the module is Drupal 11 compatible, actually it won't work unless you use a jquery compatible colorbox library.
For easy testing I attached the modified files to replace in the drupal's library folder.