添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Announcement icon Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.

I would like to add a javascript to run once after content has loaded.

$(window).on("load", function() {

How do I achieve this in Drupal 9?

I have a JS file in my theme, it gets included.

The content of that file is:

(function ($, Drupal) {
'use strict';

... some JS code ...

Drupal.behaviors.MenuDepth = {
attach: function (context, settings) {
$(context)
... some code ...

$(context)
... some code...
})(jQuery, Drupal);

How should I modify that file so as to add my javascript to run only once after content has loaded ?

You don't have to change the code, just add it to the body of the following function:

attach: function (context, settings) {}

References
1. Drupal.behaviors .
2. Understanding JavaScript behaviors in Drupal .