FAQ: Learn HTML: Script - How are scripts loaded?

This community-built FAQ covers the “How are scripts loaded?” exercise from the lesson “Learn HTML: Script”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise How are scripts loaded?

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

For some reason, whenever I switch the Javascript files, I receive an error saying “elem is not defined”. Something similar happened with the previous exercise where it said I was wrong even though my answer was exactly the same as the solution. Is there something wrong with my browser or is it due to the lesson itself?

1 Like

Same thing happened to me.

How come the script tag was used in the body and not the head in this lesson?

How does script2 communicate with script1 without any reference of script1 in script2???!!!

Barring the use of modules, JS has but one namespace. That means all script loaded will reside in the same defined area of memory. What’s more, script runs when it loads so the script that loads first will have run before the script that follows.

The scripts do not communicate with each other. JS simply has access to the functions and variables of each since they are all in one place, the namespace.

The apt pupil will foresee the potential for collisions of either variables or functions, or both. Bottom line, we really need to predict this and mitigate it. The flipside could be one script may define a global variable that the second script looks for.

Apart from how many scripts we load, then we have the consideration of plug-ins. Think back to the days when we didn’t have frameworks and polyfills. Variable collisions were a definite concern since we didn’t have modules and everything had to play nice in the same room.

    let windowInnerWidth = window.innerWidth
    let windowInnerHeight = window.innerHeight

Any script that loads and runs with a dependency on these variables will be happy to see them present in memory.

1 Like

Ahhhh that makes sense, I see… just like the lesson that explained how JS uses the tag as the door to the house putting everything in one structure!

I understand now!!!

1 Like

One should add that dependency is an Achilles Heel. If you are combining scripts, for whatever reason, then test for the existence before polling.

if (! windowInnerWidth) {
    var windowInnerWidth = window.innerWidth
    var windowInnerHeight = window.innerHeight

See immediately how redundant that is? All the same, it is not something to overlook. Redundancy is how we keep spacecraft running for decades of operation.

edit: var is used to escape block scope declaration

1 Like

So… basically if another script aka script2 is looking for a variable in specific and the script1 is called before it in the document and loaded… but also happens to have the variable in the global scope that script2 is looking for, then after script2 is loaded and is looking for the variable that script1 happens to have then it will use the variable from the global scope of script1.

Correct, or could Function and Block Scoped variables be found as well by script2 from script1?

Block scoping is the Achilles Heel of script interoperability. It all comes down to global when scripts are interacting.

1 Like