Project

General

Profile

1
// add event cross browser
2
function addEvent(elem, event, fn) {
3
    if (elem.addEventListener) {
4
        elem.addEventListener(event, fn, false);
5
    } else {
6
        elem.attachEvent("on" + event, function() {
7
            // set the this pointer same as addEventListener when fn is called
8
            return(fn.call(elem, window.event));   
9
        });
10
    }
11
}
12

    
13

    
14
var logs = [];
15
var eventSet = false;
16
var loaded = false;
17
function log(str) {
18
    
19
    if (loaded) {
20
        output(str);
21
    } else {
22
        logs.push(str);
23
    }
24

    
25
    function output(str) {
26
        var o = document.getElementById("log");
27
        var div = document.createElement("div");
28
        div.appendChild(document.createTextNode(str));
29
        o.appendChild(div);
30
    }
31
    
32
    if (!eventSet) {
33
        eventSet = true;
34
        addEvent(window, "load", function() {
35
            loaded = true;
36
            for (var i = 0; i < logs.length; i++) {
37
                output(logs[i]);
38
            }
39
            logs = [];
40
        });
41
    }    
42
}
43

    
44
(function(funcName, baseObj) {
45
    // The public function name defaults to window.domReady
46
    // but you can pass in your own object and own function name and those will be used
47
    // if you want to put them in a different namespace
48
    funcName = funcName || "domReady";
49
    baseObj = baseObj || window;
50
    var readyList = [];
51
    var readyFired = false;
52
    var readyEventHandlersInstalled = false;
53
    
54
    // call this when the document is ready
55
    // this function protects itself against being called more than once
56
    function ready() {
57
        if (!readyFired) {
58
            // this must be set to true before we start calling callbacks
59
            readyFired = true;
60
            for (var i = 0; i < readyList.length; i++) {
61
                // if a callback here happens to add new ready handlers,
62
                // the domReady() function will see that it already fired
63
                // and will schedule the callback to run right after
64
                // this event loop finishes so all handlers will still execute
65
                // in order and no new ones will be added to the readyList
66
                // while we are processing the list
67
                readyList[i].fn.call(window, readyList[i].ctx);
68
            }
69
            // allow any closures held by these functions to free
70
            readyList = [];
71
        }
72
    }
73
    
74
    function readyStateChange() {
75
    if ( document.readyState === "complete" ) {
76
            ready();
77
        }
78
    }
79
    
80
    // This is the one public interface
81
    // domReady(fn, context);
82
    // the context argument is optional - if present, it will be passed
83
    // as an argument to the callback
84
    baseObj[funcName] = function(callback, context) {
85
        // if ready has already fired, then just schedule the callback
86
        // to fire asynchronously, but right away
87
        if (readyFired) {
88
            setTimeout(function() {callback(context);}, 1);
89
            return;
90
        } else {
91
            // add the function and context to the list
92
            readyList.push({fn: callback, ctx: context});
93
        }
94
        // if document already ready to go, schedule the ready function to run
95
        if (document.readyState === "complete") {
96
            setTimeout(ready, 1);
97
        } else if (!readyEventHandlersInstalled) {
98
            // otherwise if we don't have event handlers installed, install them
99
            if (document.addEventListener) {
100
                // first choice is DOMContentLoaded event
101
                document.addEventListener("DOMContentLoaded", ready, false);
102
                // backup is window load event
103
                window.addEventListener("load", ready, false);
104
            } else {
105
                // must be IE
106
                document.attachEvent("onreadystatechange", readyStateChange);
107
                window.attachEvent("onload", ready);
108
            }
109
            readyEventHandlersInstalled = true;
110
        }
111
    }
112
})("domReady", window);
113

    
114
function confirm_link(message, url) {
115
    if(confirm(message)) location.href = url;
116
}
117

    
118

    
119
/**
120
 * 
121
// test basic functionality
122
domReady(function() {
123
    document.body.appendChild(document.createTextNode("Hello Text 1"));
124
    // test adding new domReady handler from a domReady callback
125
    domReady(function() {
126
        document.body.appendChild(document.createTextNode(", Hello Text 2"));
127
    });
128
});
129

    
130
// test finding an ID in the document
131
domReady(function() {
132
    document.getElementById("test").innerHTML = "Hello ID";
133
});
134

    
135
// test calling domReady after window load and
136
// domReady has already fired
137
addEvent(window, "load", function() {
138
    setTimeout(function() {
139
    document.body.appendChild(document.createTextNode(", Hello Text 2.5"));
140
        
141
        domReady(function(arg) {
142
            document.body.appendChild(document.createTextNode(arg));
143
        }, ", Hello Text 3");
144
    }, 1);
145
});
146
})();
147
 */
(1-1/10)