By peterk900 on
I'm struggling with Javascript functionality and want to add jQuery to every page as well as a CDN link. I thought the first could be solved by amending the main theme library - e.g.
global:
js:
core/jquery
but this doesn't work. I've sifted through a lot of posts but can find an example of either use case. (I can achieve both of these requirements by CDN url's between <script> tags on a Basic Page.)
Any help much appreciated. Thanks.
Comments
jQuery seems available in Drupal 12
I am not a JavaScript developer at all, but it seems like jQuery is still available in Drupal 12, after checking with this command in the Firefox console:
But you could share your use case here, in case someone can suggest a plain JavaScript solution?
About CDN's, see CDN / externally hosted libraries > Adding assets (CSS, JS) to a Drupal module via *.libraries.yml.
Thanks ressa
That link was useful. And what you found out about D12.
Returning to my wish to include jQuery on each page...
This script displays a jQuery dropdown in a Drupal article;
But when I remove the CDN link to jQuery and update libraries.yml, it fails, without error.
I have cloned Olivero. This is libraries.yml, where I have added a dependency on jQuery.
The .info file is unaltered...
Can anyone suggest what I'm doing wrong?
You're welcome @peterk900, I
You're welcome @peterk900, I am glad to hear it was useful.
About the theme, you write "clone", but why not create a sub-theme?
In Olivero's olivero.libraries.yml, the
dependenciesitems are only indented one level (two spaces):But, more importantly -- I do not see jQuery mentioned, maybe jQuery support is added by Drupal core?
Well spotted!
Thanks for your continued help on this.
The spacing for the dependencies was wrong. And my words were careless. I am working with an Olivero sub-theme.
I've corrected the spacing problem...
But it still doesn't work.
I understand that jQuery is part of core in D11 but jQuery UI is not. So I installed the jQuery UI module and cleared cache but, sadly, this has had no effect.
This link refers to D11, jQuery 4 and Bootstrap theme problems but as I'm not using Bootstrap, I can't see this as relevant.
I'm not sure what else to try!
Glad that it got more correct
Glad that it got more correct :) Too bad it doesn't work ...
But since jQuery seems to now be difficult to get working in Drupal 11, maybe it is better to aim for a JavaScript solution, hopefully someone has a tip about that.
Thanks
You may be right. JavaScript today has many of the features which jQuery provides. It’s just I’m used to jQuery!
Your current declaration of
Your current declaration of dependencies looks correct. I'm sure you cleared your cache after doing it, but did you clear your cache after doing it? Upon doing so, I would look at the generated HTML to verify whether the jquery library is being included. I'm not seeing any reason it shouldn't be. If it's not, this would seem like a bug, but lets start there.
Contact me to contract me for D7 -> D10/11 migrations.
Sorry for the delay in replying...
I've only just noticed your response. I'm back on the case and I'll carry out the checks you suggest.
Here's an update...
jQuery is being loaded from core...
both with and without the presence of a CDN link.
This code...
pops up the CDN version - 3.7.1.
So it seems to be that the core version of jQuery is loaded when the page is displayed but the script does not use the core code. But a CDN script, as part of page content, is processed and so the jQuery functionality works.
If there is anything I can try to throw light on what's happening, please let me know.
Update
I think the problem may be related to writing HTML/Javascript using Full HTML and in Source mode which means using CKEditor.
When I create a new text format without a text editor and set no options, I find a jQuery example script runs without the need for a CDN - which means it's using the jQuery code which comes from core. At a stroke, this may solve all the above issues! Interestingly, including a CDN link appears to have no effect.
I'll play with this a little more and post again but I'm fairly convinced CKEditor was responsible for the problem.