Git Product home page Git Product logo

domready's Introduction

Integralist - DOMready

Description

A cross browser 'DOM ready' function.

If you don't know already, (usually) the best time to trigger your JavaScript functions is when the DOM (Document Object Model) is ready to be interacted with.

If you try and call a function which modifies an element in the DOM, but yet you're not waiting for the window.onload event then the function may fire before the DOM element is available to be interacted with and your script will generate an error.

So to avoid this, developers would write code that looked like this...

window.onload = function() { // do something here };

...but the problem was that it was just too slow for most developers to put up with. You want to be interacting with your DOM as soon as possible, but if you use the window.onload event then you're not just waiting for the DOM but all its content (such as images, stylesheets, iframes etc) to load as well.

Developers needed something quicker, and so here comes the DOMContentLoaded event which fires when the browser has finished parsing the document but before the rest of the assets in the page (e.g. images, stylesheets, iframes etc) have finished loading.

Problem is this event isn't supported cross-browser so developers have come up with a multitude of ways of mimicking this.

This script is one such way.

Disclaimer

This DOMready function was originally a modified version of @ded's "Smallest DOMReady code, ever' - but it quickly became apparent (thanks to @jdalton) that this version was sorely lacking and that although the work-around did 'work' it was very slow and in some browsers only marginally quicker than a simple window.onload.

So I reverted the code back to a previous version I had from a few years back.

But since then I've had feedback from both @jdalton and @diegoperini and implemented some essential updates to make this script as effective as possible.

My previous iterations (not on Github) have included at some point or another used...

  • For versions of Safari older than 525 (which didn't support DOMContentLoaded) use the document.readyState method.
  • For Internet Explorer: using Conditional Compilation @cc_on @ along with setInterval to check the doScroll response
  • For Internet Explorer: document.write of deferred script
  • For Internet Explorer: checking for both document.body && document.body.lastChild
  • Checking for document && document.getElementsByTagName && document.getElementById && document.body

Miscellaneous notes

Dean Edwards document.write of the deferred script has given problems on some pages (causing a consistent > 60 sec delay).

Dean also suggests that document.readyState is unreliable and that he has seen cases where document.readyState was "complete" while document.body was still null. As well as other cases where document.readyState was not complete until after all images on the page were loaded.

The doScroll method has been seen to succeed while document.body is still null. And document.body can be non-null prior to the DOM being available.

One solution that so far has tested 100% OK is to combine a test for both document.body and success of doScroll. Sometimes the doScroll is not available and Internet Explorer falls back to window.onload so some developers tried using a timer to counter this.

domready's People

Contributors

integralist avatar daniel15 avatar

Stargazers

 avatar

Watchers

 avatar James Cloos 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.