Skip to content

⛄️ Run that Javascript code when the DOM is ready

License

Notifications You must be signed in to change notification settings

Tahseenm/youreadydom

Repository files navigation

Travis Build Codecov Coverage Version MIT License

you ready DOM

dogs waiting

Hold off that javascript code until Document Object Model (DOM) is safe to manipulate.

 

Table of Content

Library

youreadydom is a tiny, fully tested javascript library, written in ES6. It provides the same functionality as jQuery .ready() method without jQuery's overhead. Unlike jQuery, youreadydom is promise based—it's modern 😎. Check out the examples below.

You can add this to your list of reasons for why you might not need jquery anymore.

For those unfamiliar with jQuery .ready() method, it provides a way to run javascript code as soon as the Document Object Model (DOM) has finished loading.

'DOMContentLoaded'

Most browsers provide the 'DOMContentLoaded' event which provides similar functionality but differs in that it does not execute any listener which is added after the event is fired. Internally this library adds a single 'DOMContentLoaded' event listener only if DOM content has not already loaded. The listener is removed once the event is fired and all handlers are executed in the order they are added.

/** Comparison */
setTimeout(() => {
  document.addEventListener('DOMContentLoaded', () => {
    /** 'DOMContentLoaded' event has already fired so code doesn't run */
    alert('I will not run 😩')
  })

  domReady(() => {
    /** youreadydom works */
    alert('I will run 😊')
  })
}, 0)

window.onload

The 'load' event on window is fired when all resources on the page have finished loading. This includes images.

 

Browser Support

Chrome Safari IE* / Edge Firefox Opera
Yes Yes 9+ Yes Yes

Note: Internet Explorer is supported as long as Promise is polyfilled. Check out polyfill.io for automatic browser polyfills!

 

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies

Using npm

> npm install --save youreadydom

Using yarn

> yarn add youreadydom

Using CDN (Not recommended)

<!-- Exposes `domReady` global function -->
<script src="https://unpkg.com/youreadydom@1.0.0/dist/youreadydom.min.js"></script>

 

Examples

Callback

Lib example using Javascript callbacks

'use strict'

var domReady = require('youreadydom')
var $ = document.querySelector.bind(document)


/** Oldschool 👍 */
domReady(function something() {
  var foo = $('#foo')
  var bar = $('#bar')

  /** Do something ... */
})

Promise

Lib example using Javascript Promises

import React from 'react'
import ReactDOM from 'react-dom'
import whenDOMisReady from 'youreadydom'

import $ from './dom'
import App from './App'


const renderApp = () => ReactDOM.render(<App />, $('#root'))

/** Enjoy a promise 😃 */
whenDOMisReady().then(renderApp)

Async Function

Lib example using Javascript async functions

import domToBeReady from 'youreadydom'

import $ from './dom'
import fetchFoo from './foo.service'


/** Just `await` it 😍 */
;(async function main() {
  const data = await fetchFoo()

  await domToBeReady()

  const foo = $('#foo')
  foo.textContent = data
})()

 

Contributors

Tahseen Malik

License

MIT

About

⛄️ Run that Javascript code when the DOM is ready

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published