Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit e179e802bb3831af2bea987a2f046d991c2d9309 0 parents
airportyh authored
Showing with 81 additions and 0 deletions.
  1. +11 −0 README.md
  2. +21 −0 index.html
  3. +49 −0 onready.js
11 README.md
@@ -0,0 +1,11 @@
+OnReady
+=======
+OnReady event ala jQuery or lowpro.js in a small bundle.
+Usage:
+------
+1. Include file:
+ <script type="text/javascript" src="onready.js"></script>
+2. Use:
+ OnReady(function(){
+ alert('DOM is now ready!');
+ })
21 index.html
@@ -0,0 +1,21 @@
+<html>
+ <head>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+ <title>OnReady Test</title>
+ <script type="text/javascript" src="onready.js"></script>
+ <script type="text/javascript">
+ OnReady(function(){
+ var log = document.getElementById('log')
+ log.innerHTML += 'DOM Ready!'
+ })
+ OnReady(function(){
+ var log = document.getElementById('log')
+ log.innerHTML += 'DOM Ready 2!'
+ })
+ </script>
+ </head>
+ <body>
+ <h1>OnReady Test</h1>
+ <div id="log"></div>
+ </body>
+</html>
49 onready.js
@@ -0,0 +1,49 @@
+(function(){
+ var addLoadListener
+ var removeLoadListener
+ if (window.addEventListener){
+ addLoadListener = function(func){
+ window.addEventListener('DOMContentLoaded', func, false)
+ window.addEventListener('load', func, false)
+ }
+ removeLoadListener = function(func){
+ window.removeEventListener('DOMContentLoaded', func, false)
+ window.removeEventListener('load', func, false)
+ }
+ }else if (document.attachEvent){
+ addLoadListener = function(func){
+ document.attachEvent('onreadystatechange', func)
+ document.attachEvent('load', func)
+ }
+ removeLoadListener = function(func){
+ document.detachEvent('onreadystatechange', func)
+ document.detachEvent('load', func)
+ }
+ }
+
+ var callbacks = null
+ var done = false
+ function __onReady(){
+ done = true
+ removeLoadListener(__onReady)
+ if (!callbacks) return
+ for (var i = 0; i < callbacks.length; i++){
+ callbacks[i]()
+ }
+ callbacks = null
+ }
+ function OnReady(func){
+ if (done){
+ func()
+ return
+ }
+ if (!callbacks){
+ callbacks = []
+ addLoadListener(__onReady)
+ }
+ callbacks.push(func)
+ }
+ window.OnReady = OnReady
+})()
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.