~caneypuggies/reformedchurcheslocator/couchapp-backbone

« back to all changes in this revision

Viewing changes to _attachments/js/vendor/requirejs/tests/browsertests/scriptloadinteractive/index.html

  • Committer: Tim Black
  • Date: 2013-09-16 22:50:16 UTC
  • Revision ID: tim@alwaysreformed.com-20130916225016-zk8jiba25z33ew7h
Versioned Bower vendor directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html>
 
2
<html>
 
3
<head>
 
4
    <title>Script Load Interactive Test</title>
 
5
    <script src="../common.js"></script>
 
6
    <script>
 
7
    /*jslint plusplus: false, strict: false */
 
8
    /*global log: false, dumpLogs: false */
 
9
    //cache bust?
 
10
    var noCache = location.href.indexOf("nocache") !== -1;
 
11
 
 
12
    log("noCache: " + noCache);
 
13
 
 
14
    var readyRegExp = /complete|loaded/,
 
15
        useInteractive = false,
 
16
        loadedScripts = [],
 
17
        callCount = 0,
 
18
        currentlyAddingScript,
 
19
        waitingFunc, loadUrls,
 
20
        urls = [
 
21
            "one.js",
 
22
            "two.js",
 
23
            "three.js",
 
24
            "four.js",
 
25
            "five.js",
 
26
            "six.js",
 
27
            "seven.js",
 
28
            "eight.js",
 
29
            "nine.js"
 
30
        ];
 
31
 
 
32
    function report() {
 
33
        var i, module;
 
34
        for (i = 0; i < loadedScripts.length; i++) {
 
35
            module = loadedScripts[i];
 
36
            log("module " + module.name + " === " + module.obj.name);
 
37
        }
 
38
        callCount += 1;
 
39
        //dumpLogs();
 
40
        if (callCount === 1) {
 
41
            log("-------Trying cache hits now--------");
 
42
            loadedScripts = [];
 
43
            setTimeout(loadUrls, 500);
 
44
        }
 
45
    }
 
46
 
 
47
    function finishScript(script, func) {
 
48
        if (!script) {
 
49
            return;
 
50
        }
 
51
 
 
52
        loadedScripts.push({
 
53
            name: script.getAttribute('data-name').replace(/\.js$/, ''),
 
54
            obj: func()
 
55
        });
 
56
 
 
57
        if (loadedScripts.length === 9) {
 
58
            report();
 
59
        }
 
60
    }
 
61
 
 
62
    function onTestScriptLoad(evt) {
 
63
        var node = evt.target || evt.srcElement;
 
64
        if (evt.type === "load" || readyRegExp.test(node.readyState)) {
 
65
            if (!useInteractive) {
 
66
                finishScript(node, waitingFunc);
 
67
            }
 
68
 
 
69
            //Clean up
 
70
            if (node.removeEventListener) {
 
71
                node.removeEventListener("load", onTestScriptLoad, false);
 
72
            } else {
 
73
                //Probably IE.
 
74
                node.detachEvent("onreadystatechange", onTestScriptLoad);
 
75
            }
 
76
        }
 
77
    }
 
78
    function attachScript(url, name, useDocWrite) {
 
79
        if (noCache) {
 
80
            url += "?stamp=" + (new Date()).getTime();
 
81
        }
 
82
        var node = document.createElement("script");
 
83
        node.src = url;
 
84
        node.type = "text/javascript";
 
85
        node.charset = "utf-8";
 
86
        node.setAttribute("data-name", name);
 
87
 
 
88
        //Set up load listener.
 
89
        if (node.addEventListener) {
 
90
            node.addEventListener("load", onTestScriptLoad, false);
 
91
        } else {
 
92
            //Probably IE.
 
93
            useInteractive = true;
 
94
            node.attachEvent("onreadystatechange", onTestScriptLoad);
 
95
        }
 
96
        currentlyAddingScript = node;
 
97
 
 
98
        document.getElementsByTagName("head")[0].appendChild(node);
 
99
        currentlyAddingScript = null;
 
100
    }
 
101
 
 
102
    loadUrls = function () {
 
103
        for (var i = 0, url; (url = urls[i]); i++) {
 
104
            attachScript(url, url);
 
105
        }
 
106
    };
 
107
 
 
108
    function def(func) {
 
109
        var scripts, i, script = currentlyAddingScript;
 
110
        if (useInteractive) {
 
111
            scripts = document.getElementsByTagName('script');
 
112
            var states = [];
 
113
            for (i = scripts.length - 1; i > -1; i--) {
 
114
                states.push(i + scripts[i].readyState + scripts[i].src);
 
115
                //log('script with name ' + scripts[i].getAttribute('data-name') + ' has readyState = ' + scripts[i].readyState + ' for func: ' + func);
 
116
                if (scripts[i].readyState === 'interactive') {
 
117
                    script = scripts[i];
 
118
                    break;
 
119
                }
 
120
            }
 
121
            if (!script) {
 
122
                log("ERROR: No matching script interactive for " + func);
 
123
                log("script readyStates are: " + states);
 
124
            }
 
125
            //log('calling finishScript for interactive dat-name: ' + script.getAttribute('data-name'));
 
126
            finishScript(script, func);
 
127
        } else {
 
128
            waitingFunc = func;
 
129
        }
 
130
    }
 
131
 
 
132
    //Do appendChilds
 
133
    loadUrls();
 
134
 
 
135
    </script>
 
136
</head>
 
137
<body>
 
138
    <h1>Script Load Interactive Test</h1>
 
139
 
 
140
    <p>This test checks to see if a function call can be associated with a specific script tag.</p>
 
141
    <p>For non-IE 6-8 browsers, the script onload event may not fire right after the the script
 
142
    is evaluated. Kris Zyp found for IE though that in a function call that is called while the
 
143
    script is executed, it could query the script nodes and the one that is in "interactive" mode
 
144
    indicates the current script.</p>
 
145
    <p>So this test tries to see to use interactive state if possible, and if that does not work,
 
146
    falls back to using script onload to associate the scripts.</p>
 
147
    <p>Check the console for output. Expected result, all scripts are matched up with their calls.</p>
 
148
</body>
 
149
</html>