Coder Social home page Coder Social logo

fewpjs-document-onload-lab-debug-batch's Introduction

Loading JavaScript on Document Load

Learning Goals

  • Why DOMContentLoaded is important?
  • Set up an event on DOMContentLoaded
  • Reiterate separation of concerns for JavaScript

Introduction

An important part of working with JavaScript is ensuring that your code runs at the right time. Every now and then, you may have to add some extra code to ensure your code doesn't run before the page is ready. There are many factors that go into determining the "right time". There are two events that represent two important milestones in terms of page load:

  1. DOMContentLoaded event fires at when your page's DOM is fully parsed
  2. The load event fires when a resource and its dependent resources (CSS, JavaScript) have finished loading

In this lesson, we'll be focusing on DOMContentLoaded.

Why is DOMContentLoaded is Important?

The browser has a built-in way to determine when a page is loaded. The DOMContentLoaded event is triggered when the page’s DOM is ready--or finished loading. This means style and HTML is ready to receive requests from JavaScript. You can't bind events to HTML elements that haven't rendered yet. As such, we want to perform event binding only after we're sure the DOM...Content....has.....Loaded.

As always, addEventListener takes a String with the name of the event and a callback function.

document.addEventListener("DOMContentLoaded", domLoaded, false);

function domLoaded(event) {
  alert("The DOM has loaded");
}

Set Up an Event on DOMContentLoaded

You'll be coding in index.js. In index.js, we need to set up a DOMContentLoaded event in order to detect when our HTML page has loaded, and the document is ready to be manipulated. Use the event to target the paragraph with id="text" and replace the text with "This is really cool!"

Once you have confirmed that this working by viewing the results in index.html, let's create a wrapper for this function called domContentLoaded. Next, create a function called updateDOM and place the code for updating the dome in this block. Your domContentLoaded function will call updateDOM() inside of the eventListener.

Reiterate Separation of Concerns for JavaScript

We usually want to avoid writing 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. When code is well-separated, individual code can be reused, modified, maintained, and debugged easier. To ensure proper load order of all of the code, place your external stylesheets in the <head> of your HTML document and your external JavaScript before the losing <body> tag. This approach forces the JavaScript to execute only after the CSS and HTML body have been loaded. If you are placing stylesheets in the header and JavaScript at the end of the HTML document, your DOM will load all everything before the DOMContentLoaded event is fired.

Conclusion

In many cases, you'll want to execute events as soon as a page loads. The DOMContentLoaded event is a useful event that can make a big impact on the performance of your pages, as well as it'll be useful in a number of scenarios for user interaction and content manipulation.

Resources

Running Your Code at the Right Time

fewpjs-document-onload-lab-debug-batch's People

Contributors

drakeltheryuujin avatar sgharms avatar

Watchers

Kevin Ernest Long avatar James Cloos avatar Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar vera protopopova avatar  avatar Joe Cardarelli avatar Sam Birk avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar Vishal Patel avatar  avatar Jaichitra (JC) Balakrishnan avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar  avatar Lore Dirick avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  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.