npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dhvagna-dom

v1.2.0

Published

Lightweight utility to ensure JavaScript code runs after DOM is fully loaded

Readme

dhvagna-dom

What makes dhvagna-dom stand out? Unlike other DOM-ready utilities, this package combines TypeScript support, minimal size (0.7KB), intelligent state detection, and cross-browser compatibility while offering features like optional timeouts and proper event cleanup. It's the smartest choice for modern web development when you need reliable DOM-ready detection without the bloat.

A lightweight utility (less than 1KB) to ensure your JavaScript code runs after the DOM is fully loaded, solving the common issue where developers face issues accessing DOM elements due to scripts running before the DOM is fully loaded.

Top Reasons to Choose dhvagna-dom

| Why dhvagna-dom is the top priority choice | Advantage over similar solutions | |---------|-------------------------| | Smallest possible footprint | 5-10x smaller than alternatives like jQuery (0.7KB vs 30KB+) | | Modern TypeScript implementation | Built from the ground up with TypeScript, not just type definitions added later | | Smart state detection | Intelligently checks if DOM is already loaded before attaching events | | Clean event handling | Properly removes event listeners after firing to prevent memory leaks | | Optional timeout fallback | Unique feature not available in most alternatives | | Framework-agnostic | Works with React, Vue, Angular or vanilla JavaScript without conflicts |

Why Use dhvagna-dom?

| Problem | Solution with dhvagna-dom | |---------|-------------------------| | Scripts run before DOM is ready | Ensures your code executes at the perfect moment | | Errors like Cannot read property of null | Guarantees DOM elements exist before accessing them | | Complex workarounds and event handling | Simple, clean API that handles all edge cases | | Performance concerns with heavy libraries | Ultra-lightweight (<1KB) with zero dependencies | | Browser inconsistencies | Works reliably across all modern browsers |

Unlike bulky frameworks that include DOM-ready functionality as part of a larger package, dhvagna-dom does one thing exceptionally well. It provides the most efficient and reliable way to execute JavaScript code when the DOM is fully loaded and ready for manipulation.

How It Works Internally

dhvagna-dom uses a multi-layered approach to ensure maximum reliability:

  1. State Detection: First checks if the DOM is already loaded by examining document.readyState
  2. Event Subscription: If not already loaded, attaches listeners to both:
    • DOMContentLoaded (fires when HTML is fully parsed)
    • load (fallback that fires when all resources are loaded)
  3. Cleanup: Properly removes event listeners once triggered
  4. Safety Timeout: Optional timeout parameter as a fallback mechanism

This approach ensures your callback runs at the earliest possible moment when the DOM is ready, but never before.

The Problem

Have you ever encountered errors like this?

// This fails because the element doesn't exist yet
const element = document.getElementById('my-element');
element.innerHTML = 'Hello World'; // TypeError: Cannot set property 'innerHTML' of null

Installation

You can install dhvagna-dom using npm, yarn, or pnpm:

# Using npm
npm install dhvagna-dom

# Using yarn
yarn add dhvagna-dom

# Using pnpm
pnpm add dhvagna-dom

You can also find the source code on GitHub: https://github.com/gnanesh-16/dhvagna-dom

Usage

Basic Usage

import dhvagnaDom from 'dhvagna-dom';

dhvagnaDom(() => {
  // Your code here will run when the DOM is fully loaded
  const element = document.getElementById('my-element');
  element.innerHTML = 'Hello World';
});

With TypeScript

The package includes TypeScript definitions out of the box:

import { dhvagnaDom } from 'dhvagna-dom';

dhvagnaDom(() => {
  const element = document.getElementById('my-element') as HTMLElement;
  element.innerHTML = 'Hello World';
});

With Timeout Option

You can specify a timeout as a safety measure:

import dhvagnaDom from 'dhvagna-dom';

dhvagnaDom(
  () => {
    // Your code here
    console.log('DOM is ready or timeout reached');
  }, 
  { timeout: 3000 } // Will execute after 3 seconds even if DOM isn't ready yet
);

In Browser via CDN

<script src="https://unpkg.com/dhvagna-dom/dist/dhvagna-dom.min.js"></script>
<script>
  window.dhvagnaDom(() => {
    // Your code here
    console.log('DOM is ready!');
  });
</script>

Key Features

  • Tiny Footprint: Less than 1KB minified and gzipped (0.7KB to be exact)
  • Zero Dependencies: No external libraries required
  • TypeScript Support: Built with TypeScript for better developer experience
  • Framework Agnostic: Works with any JavaScript framework or vanilla JS
  • Smart Detection: Checks if DOM is already loaded before attaching listeners
  • Multiple Listeners: Can be called multiple times without conflicts
  • Universal Browser Support: Works in all modern browsers and IE11+

Performance Metrics

| Metric | Value | |--------|-------| | Size (minified) | ~0.9KB | | Size (minified + gzipped) | ~0.7KB | | Execution time | <1ms in most cases | | Memory footprint | Negligible | | Browser support | All modern browsers + IE11 |

Technical Comparison

| Feature | dhvagna-dom | jQuery | Plain DOMContentLoaded | defer attribute | |---------|-----------|--------|--------------------------|-------------------| | Size | 0.7KB | 30KB+ | 0KB (built-in) | 0KB (HTML attribute) | | Handles all edge cases | ✅ | ✅ | ❌ | ❌ | | Works if added after DOM ready | ✅ | ✅ | ❌ | N/A | | No dependencies | ✅ | ❌ | ✅ | ✅ | | TypeScript support | ✅ | Requires @types | ✅ | N/A | | Optional timeout | ✅ | ❌ | ❌ | ❌ |

Browser Compatibility

| Browser | Supported Versions | |---------|-------------------| | Chrome | 9+ | | Firefox | 4+ | | Safari | 5+ | | Edge | All versions | | Opera | 10+ | | IE | 11+ | | iOS Safari | 5+ | | Android Browser | 4+ |

Similar Libraries

Some alternatives to dhvagna-dom include:

  • domready: The original DOM ready utility
  • jquery.ready: jQuery's DOM ready functionality
  • document-ready: Another small DOM ready utility

The main advantage of dhvagna-dom over these alternatives is its extremely small size, TypeScript support, and modern implementation.

License

MIT