source: Dev/branches/rest-dojo-ui/client/dijit/tests/_loadTest.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.3 KB
Line 
1(function(){
2var file;
3var head = document.documentElement.firstChild;
4while(head && head.tagName != "HEAD"){
5        head = head.nextSibling;
6}
7var script = head.lastChild;
8while(script){
9        if(script.tagName == "SCRIPT"){
10                if((script.getAttribute('src')||'').search('_loadTest') >= 0 && (!script.readyState || script.readyState == "interactive")){
11                        file = script.getAttribute('file');
12                        break;
13                }
14        }
15        script = script.previousSibling;
16}
17if(!file && window.location.href.search(/[?&]file[=]/i) > 0){
18        file = window.location.href.replace(/.*[?&]file=(([^&?]*)).*/i, "$2");
19}
20var readFile = function(file){
21        var xhr = null;
22        try{
23                xhr = new XMLHttpRequest();
24        }catch(e0){
25                try{
26                        xhr = new ActiveXObject('Msxml2.XMLHTTP');
27                }catch(e1){
28                        try{
29                                xhr = new ActiveXObject('Microsoft.XMLHTTP');
30                        }catch(e2){
31                                try{
32                                        xhr = new ActiveXObject('Msxml2.XMLHTTP.4.0');
33                                }catch(e3){
34                                }
35                        }
36                }
37        }
38        try{
39                xhr.open("GET", file, false);
40                xhr.send(null);
41        }catch(e){
42                return null
43        } // file not found
44        return xhr.responseText;
45};
46var text = readFile(file) || (file + " not found");
47var baseHref = file.replace(/^(.*\/)?[^\/]+$/, "$1");
48if(baseHref){
49        baseHref = window.location.href.replace(/[?].*/, "").replace(/[^\/]*$/, "")+baseHref;
50        text = text.replace(/(<HEAD\b([^>]|\s)*>)/i, "$1" + "<BASE href='" + baseHref + "'><\/BASE>");
51}
52// strip DOCTYPE and HTML tag
53text = text.replace(/^(.|\s)*?<html\b(([^>]|\s)*)>((.|\s)*)/i,
54        function(s,a1,htmlAttrs,a3,content){
55                // add attributes from target file's HTML tag - may not be necessary but we'll do it anyway for completeness
56                htmlAttrs = htmlAttrs.replace(/((\w+)\s*=\s*(['"]?)(.*?)(\3)?(\s+|$))/g,
57                        function(s, all, attr, quote, val){
58                                document.documentElement.setAttribute(attr, val);
59                                return "";
60                        });
61                return content.replace(/<\/html\b([^>]|\s)*>(.|\s)*?$/i, "");
62        });
63if(/MSIE/.test(navigator.userAgent)){ // need to load scripts serially
64        document._oldgetElementsByTagName_ = document.getElementsByTagName;
65        document.getElementsByTagName = function(tag){
66                // take over getElementsByTagName so I can take over script.getAttribute('src')
67                if(/^script$/i.test(tag)){
68                        var scripts = document.scripts;
69                        for(var i=0; i <scripts.length; i++){
70                                var script = scripts[i];
71                                if(!script['_oldGetAttribute']){
72                                        var src = script.getAttribute('_oldsrc');
73                                        if(src){
74                                                script._oldGetAttribute = script.getAttribute;
75                                                script.getAttribute = function(attr){ if(/^src$/i.test(attr))attr='_oldsrc';return script._oldGetAttribute(attr) };
76                                        }
77                                }
78                        }
79                        return scripts;
80                }
81                return document._oldgetElementsByTagName_(tag);
82        };
83        document._oldwrite_ = document.write;
84        document.write = function(text){
85                text = text.replace(/<[!][-][-](.|\s){5,}?[-][-]>/g, "<!--?-->" // shorten long comments that may contain script tags
86                        ).replace(/(<script\s[^>]*)\bsrc\s*=\s*([^>]*>)/ig,
87                function(s,pre,post){
88                        if(s.search(/\sdefer\b/i) > 0){ return s; }
89                        //if(s.search(/\bxpopup.js\b/i) > 0){ return pre+">"; } // firewall popup blocker:  uncomment if you get out of stack space message
90                        var file = post.substr(0, post.search(/\s|>/)).replace(/['"]/g, "");
91                        var scriptText = readFile(baseHref+file);
92                        if(!scriptText){
93                                scriptText = readFile(file);
94                                if(!scriptText){ return s; }
95                        }
96                        return pre + "  _oldsrc=" + post + "eval(unescape('"+escape(scriptText)+"'))";
97                });
98                document._oldwrite_(text);
99        };
100}
101document.write(text);
102})();
Note: See TracBrowser for help on using the repository browser.