Giter Club home page Giter Club logo

jsdom-mod-document-domcontentloaded-lab's Introduction

Document Ready

Problem Statement

When we first learned about events, it made sense to talk about events in terms of things users do: clicking or mousing or typing.

But JavaScript events can also be less obvious.

  • "When we fetched some data"
  • "When the window appeared"
  • "When the DOM's data finished updating the screen"

We're going to learn more about that last one. That event is called DOMContentLoaded. It's a good way to set up learning more about the AJAX technique (update the DOM based on fetched data) that we'll cover later.

Objectives

  1. Explain why DOMContentLoaded is important
  2. Set up an event on DOMContentLoaded

Explain Why DOMContentLoaded Is Important

We don't ever want to write our JavaScript inside our HTML files. For the same reasons that we want to separate out our CSS from our HTML, we want to separate out our JavaScript from our HTML, too.

Letting Down Obi-Wan

Don't let Obi-Wan down: separate structure (HTML) and behavior (JavaScript)

But if we define JavaScript code in a file included in the <head> tag, and that JavaScript code tries to "bind an event to some HTML element," well, that element doesn't exist yet. Remember, the browser's still processing the <head>, it's not started loading up the <body> yet. As a result, we're going (try) to bind an event to...nothing.

This can be really confusing and hard to debug. Things will look as they ought on screen. But the events will not have been bound. You might not even see the error message if your DevTools Console isn't open.

In this lesson we'll experience this bug, move to a simple solution, and then use the solution based on the DOMContentLoaded event.

Set Up An Event On DOMContentLoaded

Part I:

If you take a look at index-demo.html, you'll notice we have event-handling code JavaScript code written at the bottom. We know the HTML element we want to target, body, has to be present because it appears before the <script> tag in which our JavaScript appears.

Let's see how this works.

Fire up a web server with the httpserver command. Open the index-demo.html (something like: http://<address>/index-demo.html), page in the browser and open the DevTools console. Clicking anywhere in the body should trigger a message to the console.

View the page and confirm the JavaScript code works. Click on the words to make sure you're inside the body element.

Part II:

Next, move this code into a new file called script.js. Include script.js in the <head> of index-demo.html. Reload the page. Your click event won't work. You might notice JavaScript giving an error in the DevTools console:

Uncaught TypeError: Cannot read property 'addEventListener' of null

Here the browser is telling us it tried to add a listener to an event that didn't exist, <body>. We want to attach our listeners only after we know that the <body> has fully loaded. We do this by setting up an event listener for an event called DOMContentLoaded. Inside of that event's handler, we know it's safe to bind to HTML elements in the <body>.

Edit the code in script.js to "wrap" the code inside of an event handler for DOMContentLoaded.

As you can see, the wrapping code is the same handler style you've used for all the other DOM elements. This time, though, you're binding to the DOM itself.

document.addEventListener("DOMContentLoaded", e => {
  document.querySelector("body")
   .addEventListener("click", e => console.log("Reggae, Reggae!"));
})

Go back to your browser and refresh the content. Your listener now works (again).

Moving On

Let's practice some of the ideas in this lesson on our own. While we've been dealing with click events lately, let's not forget that we know how to modify the DOM. Tracking click events might even be your path to Internet fame and fortune!

We've provided you a failing test that you need to fix. Run learn to see the output from the test. Add code to pass the test. Once your code passes the test, enter learn submit and move on.

Conclusion

In this lesson you learned to bind events to DOM events that aren't user controlled. This will allow you to operate on the DOM with the guarantee that the DOM nodes you need are there.

jsdom-mod-document-domcontentloaded-lab's People

Contributors

annjohn avatar c1505 avatar danl12186 avatar invasivelionfish avatar ipc103 avatar jessrudder avatar onyoo avatar pletcher avatar sgharms avatar victhevenot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.