FAQ: Learn HTML: Script - Defer attribute

This community-built FAQ covers the “Defer attribute” 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 Defer attribute

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!

This question isn’t about the purpose of the exercise. The “defer” tag works fine. The rest of the exercise confuses me. I assume there’s a bug somewhere but I can’t figure out where it is.

In the instructions for this exercise it states

Each script tag re-styles the Codecademy logo, but the turnYellow.js executes last, making the font color 'yellow'

I’m wondering why the script isn’t working (i.e. the font color doesn’t turn yellow). I clicked run, and the code (I didn’t write the code - it was pre-coded) looks like it should work:

var elem = document.getElementById('logo');
   	  elem.style.color = "yellow";

It seems to linked properly in the html file:

<head>
    <link rel="stylesheet" href="style.css">
    <script id="blue" src="turnBlue.js" ></script>
   <script id="yellow" src="turnYellow.js"></script>
  </head>

Anyone know why it doesn’t work?

According to the Chrome Dev Console, the default color is pink, not yellow. Maybe a mistake?

Same bug over here. Would also like to know why it doesn’t turn yellow on a run without the defer tag.

When looking in the console I get the following errors:

api.segment.io/v1/p:1 Failed to load resource: net::ERR_BLOCKED_BY_CLIENT
js-agent.newrelic.com/nr-spa-1118.min.js:1 Failed to load resource: net::ERR_BLOCKED_BY_CLIENT
geo.qualaroo.com/json/:1 Failed to load resource: net::ERR_BLOCKED_BY_CLIENT
api.segment.io/v1/m:1 Failed to load resource: net::ERR_BLOCKED_BY_CLIENT
turnBlue.js:2 Uncaught TypeError: Cannot read property 'style' of null
    at turnBlue.js:2
turnYellow.js:2 Uncaught TypeError: Cannot read property 'style' of null
    at turnYellow.js:2
1 Like

Hello, i have the same problem here, run the exercice doesnt make yellow or blue. Use defer make it run on both element ( blue or yellow) but why this doesn’t work simply and need to use defer for make one of script work ?

I think it’s because this is happening, in this order:

  1. the stylesheet is loaded
  2. turnblue.js is loaded
  3. turnyellow.js is loaded
  4. the html, including the <p> tag is loaded

When we run the code, I don’t think steps 2 and 3 apply anything, because the DOM and thus the <p> tag are not yet loaded.

So, at step 4, the <p> tag is loaded by the browser’s parser and is thus given the styling from the stylesheet loaded in step 1.

Thus we can apply defer to either or both .js files, and the <p> tag will take on the new colour as expected.

Correct me if what I write below is wrong.

I think there is one important thing about the defer attribute missing from the Learn section of this lesson. The defer attribute allows the HTML parser to continue parsing the elements below the <script> at the same time as loading the JavaScript file referred to by the src attribute of <script>.

Otherwise, there is no point of using the defer attribute instead of placing the <script> elements at the end of the HTML file.

This point is not clear at all from the Learn section of this lesson. This FAQ, linked from the lesson, hints at it by saying

“Using defer like this is potentially faster than using the <script> at the end of the body, as it loads the <script> asyncronosly”

This point then suddenly shows up in the next lesson on the async attribute by saying,

“similar to the defer attribute, the HTML parser will continue parsing the rest of the HTML as the script is downloaded in the background”.

This is quite confusing a way of explaining the defer attribute…

1 Like