Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Importing module of scripts and onload DOM

What is the difference between doing this:

import $ from "jquery";

import { getCurrentYear } from "../utils/global/functions";

$("#year").text(getCurrentYear());

and this:

import $ from "jquery";

import { getCurrentYear } from "../utils/global/functions";

function setCopyrightYear() {
  $("#year").text(getCurrentYear());
}

$(setCopyrightYear);

If I import both modules in the head section of my HTML page like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<head>
    <script type="module" src="../js/scripts/home.js"></script>
</head>

>Solution :

Passing a function to $ is equivalent to the ready method which:

run JavaScript code as soon as the page’s Document Object Model (DOM) becomes safe to manipulate

However, a <script> with type=module is a module so:

There is no need to use the defer attribute (see <script> attributes) when loading a module script; modules are deferred automatically.

… which has approximately the same effect, making the use of $(callback) pointless.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading