abstractkitchen.com logo

JavaScript Document Onload + TypeScript version

Posted by Dmitry

Updated on

🗃 This article is a part of the JavaScript Basics series.
javascript browser DOM snippet basics

Here is a straightforward way to catch document loading. It's supported by modern browsers. You can use it as an alternative to jQuery $.ready.

function domReady(cb) {
  if (document.readyState === "complete" || document.readyState === "interactive") {
    cb();
  } else {
    document.addEventListener("DOMContentLoaded", cb);
  }
}


domReady(() => {
  console.log("DOM: I'm ready");
});

TypeScript Version

function domReady(cb: Function): void {
  if (document.readyState === "complete" || document.readyState === "interactive") {
    cb();

  } else {
    document.addEventListener("DOMContentLoaded", (event: Event) => {
      cb();
    });
  }
}
Keep in mind that DOMContentLoaded fires when HTML is loaded and parsed. It's not waiting for external stylesheets and scripts to load. You can consider using window load event.

A Short Guide To The Chinese Coordinate System

Have your ever searched google maps china offset? Most people who was in China yes. Here is my story behind this question.

The Definitive Guide To Sitemaps With Python

open source python

Sitemaps are important. Especially for big websites. It is always a good idea to develop your website with SEO in mind. Unfortunately, most developers ignore this part.

How to Import CSV Files to PostgreSQL with Pgfutter

csv postgresql

Sometimes I need to upload large CSV files to PostgreSQL. CSV file might have hundreds of columns, that's why i want a tool that can do some magic for me.

How to Upload Files to DigitalOcean Spaces with Python

digitalocean python

Snippet on how to upload files to DigitalOcean Spaces with boto3 and python.

Flask Boilerplate and Your Guide to Flask in 2022. With SQLAlchemy.

boilerplate open source flask

Flask-Backbone is my take to create an initial structure and a set of rules, so most of my flask projects are easy to support.

How to Copy Text to Clipboard With Javascript

javascript DOM browser

Here is a short snippet on how to copy text to the clipboard with Javascript. Your visitors will thank you. Most likely not, but here we are.

How to Upload Files To Bunny.net Storage

python bunny_net storage snippet

Bunny.net is a well-known CDN and storage among developers. Here is my python snippet on how to upload a file to the storage.

How to Create Jinja2 Filters in Flask

python flask jinja2 snippet

In this post, I'll talk about filters. Jinja2 has a list of built-in filters, and Flask leverages them.