-
Notifications
You must be signed in to change notification settings - Fork 510
/
DOMReady.php
453 lines (374 loc) · 14.1 KB
/
DOMReady.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<?php
// http://www.webcheatsheet.com/PHP/get_current_page_url.php
function getCurrentURL(){
$pageURL = 'http' . (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? 's' : '');
$pageURL .= "://" . $_SERVER["SERVER_NAME"];
if ($_SERVER["SERVER_PORT"] != "80") $pageURL .= ":".$_SERVER["SERVER_PORT"];
return $pageURL.$_SERVER["REQUEST_URI"];
}
// http://forum.jquery.com/topic/implementation-of-domcontentloaded-failing-when-no-assets
// by andrea.giammarchi on 13-Jul-2008 10:21 PM
function flushPause($pause = 0){
echo ob_get_clean();
@ob_flush();
flush();
usleep($pause * 1000000);
}
ob_start();
ob_implicit_flush(false);
flushPause();
?>
<!DOCTYPE html>
<html>
<head>
<script>
var testElement = document.createElement('div'),
hasOperationAborted = (
testElement.innerHTML = '<!--[if lt IE 8]>1<![endif]-->', !!+testElement.innerText
);
var MESSAGES = document.createElement('div')
, thingsThatHappened = {}
function somethingHappened(id, result){
if (window.ONLOAD) document.body.insertBefore(MESSAGES, document.body.firstChild)
if (typeof result == 'function') result = result()
if (result == null) result = ''
if (result === true) result = 'PASS'
if (result === false) result = 'FAIL'
if (thingsThatHappened[id] === result) return
thingsThatHappened[id] = result
log((+new Date - START_TIME) +' '+ id + ' ' + result)
MESSAGES.innerHTML
+= '<p id="' + id + '" class="' + result + '">'
+ '<b>' + (+new Date - START_TIME) + 'ms </b>'
+ id
+ ' '
+ result
}
var START_TIME = +new Date
function isNotLoaded(){ return !!(window.PARSED && !window.ONLOAD) }
if (false <?php if (isset($_GET['iframe'])) echo '|| true'; ?>){
try {
document.domain = 'localhost'
somethingHappened("document.domain = 'localhost'", true)
}
catch(e) {
somethingHappened("document.domain = 'localhost'; " + e, false)
}
}
var Browser = {}
function log(message){
try {
console.log('' + message)
}
catch (e){}
}
var domreadyCallbacks = []
function DomReady(fn){
domreadyCallbacks.push(fn)
}
<?php if (!isset($_GET['plain'])):
$core = dirname(__FILE__) . '/../../../';
require $core . 'Packager/packager.php';
$pkg = new Packager(array($core));
echo $pkg->build(array('DOMReady'), array(), array(), array());
else: ?>
document.write('<scr'+'ipt src="./DOMReady.js?' + (new Date) + '"><'+'/script>');
document.write('<scr'+'ipt src="../../../Source/Utilities/DOMReady.js?' + (new Date) + '"><'+'/script>');
<?php endif; ?>
</script>
<script>
var loadScript = function(type){
var count = 0;
new Element('script', {
src: '../../Configuration.js?' + type + (new Date),
events: {
load: function(){
log('>> ' + type + ': JavaSript Loaded, load called: ' + (++count));
}
}
}).inject(document.body);
log('>> ' + type + ': Loading JavaScript');
};
window.addEvent('load', function(){
loadScript('load');
window.LOADED = true
somethingHappened('<i>MooTools load</i>', function(){
return !!window.READY
})
});
window.addEvent('domready', function(){
loadScript('domready');
window.READY = +new Date
somethingHappened('<i>MooTools domready</i>', isNotLoaded)
});
DomReady(function(){
window.READY = +new Date
somethingHappened('<i>MooTools domready</i>', isNotLoaded)
});
</script>
<meta http-equiv=Content-type content="text/html; charset=utf-8">
<title>DomReady Test</title>
<style>
iframe {
position: absolute;
top: 0px;
right: 0px;
height: 2000px;
width: 500px;
}
body{
padding-right:500px;
}
html.framed body{
padding-right:0;
}
html.framed{
background:#eee;
}
html, body, td, th{
font: 11px "Lucida Grande", "Trebuchet MS", Verdana, sans-serif;
}
p{
margin:0 !important;
padding: 1ex 1em;
}
i{
font-size: 125%;
}
.PASS{background:#0f0;}
.FAIL{background:#f00;}
.Yes{background:#cfc;}
.No{background:#fcc;}
small{
font-size: 9px;
}
</style>
<script>
if (document.addEventListener) document.addEventListener('DOMContentLoaded', function(){ window.READY = +new Date; somethingHappened('DOMContentLoaded (addEventListener)', isNotLoaded) }, false)
if (document.attachEvent) document.attachEvent('onDOMContentLoaded', function(){ window.READY = +new Date; somethingHappened('DOMContentLoaded (attachEvent)', isNotLoaded) }, false)
if (document.addEventListener) document.addEventListener('readyStateChange', function(){ somethingHappened('readyStateChange (addEventListener)', document.readyState) }, false)
if (document.attachEvent) document.attachEvent('onReadyStateChange', function(){ somethingHappened('onReadyStateChange (attachEvent)', document.readyState) }, false)
if (document.attachEvent) document.attachEvent('onreadystatechange', function(){ somethingHappened('onreadystatechange (attachEvent)', document.readyState) }, false)
var TEST_ELEMENT = document.createElement('div')
function pollDoScroll(){
if (!TEST_ELEMENT.doScroll) return
var PASS
try {
TEST_ELEMENT.doScroll('left')
PASS = true
}
catch (e){
PASS = false
}
window.CANSCROLL = PASS
somethingHappened('TEST_ELEMENT.doScroll()', PASS)
if (!PASS) setTimeout(pollDoScroll, 10)
}
function pollDoScroll_body(){
if (!document.body.doScroll) return
var PASS
try {
document.body.doScroll('left')
PASS = true
}
catch (e){
PASS = false
}
somethingHappened('document.body.doScroll()', PASS)
if (!PASS) setTimeout(pollDoScroll_body, 10)
}
var lastReadyState
function pollReadyState(){
var readyState = document.readyState
if (!readyState) return
if (readyState == lastReadyState) return
somethingHappened('poll document.readyState', readyState)
if (readyState != 'complete' && readyState != 'loaded') setTimeout(pollReadyState, 10)
lastReadyState = readyState
}
function pollBodyExists(){
var PASS
try {
document.body.lastChild
PASS = true
}
catch (e){
PASS = false
}
somethingHappened('body Exists?', PASS? 'YES':'NO')
if (!window.ONLOAD) setTimeout(pollBodyExists, 10)
}
function pollAugmentBody(){
var PASS
, body = document.body
, root = body.parentNode
, sibling = body.nextSibling
try {
body.appendChild(document.createTextNode( new Date - START_TIME + 'ms:Body ') )
PASS = true
}
catch (e){
PASS = false
}
somethingHappened('can Augment Body?', PASS? 'YES':'NO')
if (!window.ONLOAD) setTimeout(pollAugmentBody, 10)
}
// // // // // // // // // // // // // // // // // // // // //
var readyTests = {
"document.readyState ==": function(){return document.readyState}
,'document.body exists?': function(){return document.body ?'Yes':'No'}
,"All page content loaded and parsed?": function(){return window.PARSED ?'Yes':'No'}
,"cached IMG onload fired?": function(){return window.IMG_ONLOAD ?'Yes':'No'}
,"uncached IMG onload fired?": function(){return window.IMG_ONLOAD_UNCACHED ?'Yes':'No'}
,"document ready?": function(){return !!window.READY ?'Yes':'No'}
,"onload fired?": function(){return !!window.ONLOAD ?'Yes':'No'}
,"el.doScroll()": function(){
try {
TEST_ELEMENT.doScroll()
return 'Yes'
}
catch (e){
return 'No'
}
}
,"body.doScroll('left')": function(){
try {
document.body.doScroll('left')
return 'Yes'
}
catch (e){
return 'No'
}
}
,'isFramed?': function(){
return isFramed() ?'Yes':'No'
}
,'Is top frame?': function(){
return window.window === window.top ?'Yes':'No'
}
}
if (!hasOperationAborted) readyTests['Can augment body?'] = function(){
try {
document.body.appendChild(
document.createTextNode('Augmented the body! ' + (+new Date - START_TIME))
)
document.body.removeChild(document.body.lastChild)
return 'Yes'
}
catch (e){
return 'No'
}
}
var readyTestResults = []
function poll(){
var results = {}
, lastResults = readyTestResults[readyTestResults.length - 1] || {}
, hasDifferentResults = 0
results.ms = new Date - START_TIME
for (var id in readyTests){
results[id] = readyTests[id]()
if (results[id] == lastResults[id]) continue
++ hasDifferentResults
somethingHappened(id, results[id])
}
var shouldBeReady
if (window.CANSCROLL && !isFramed()) shouldBeReady = true
if ({loaded:1,complete:1}[document.readyState]) shouldBeReady = true
if (window.LOADED) shouldBeReady = true
if (window.IMG_ONLOAD_UNCACHED) shouldBeReady = true
if (shouldBeReady)
somethingHappened('Should be Ready!', function(){
return !!window.READY ?true:'Not yet...'
})
if (hasDifferentResults) readyTestResults.push(results)
if (!window.ONLOAD) setTimeout(poll, 10)
else report()
}
function isFramed(){
try {
return window.frameElement != null
} catch(e) {
return true
}
}
function report(){
var EL = document.createElement('div')
, HTML = '<table class=results>'
for (var i = 0; i < readyTestResults.length; ++i){
if (i == 0){
HTML += '<thead><tr>'
for (var key in readyTestResults[i]){
HTML += '<th>'
HTML += key
HTML += '</th>'
}
HTML += '</tr></thead>'
continue
}
HTML += '<tr>'
for (var key in readyTestResults[i]){
HTML += '<td>'
HTML += readyTestResults[i][key]
HTML += '</td>'
}
HTML += '</tr>'
}
EL.innerHTML = HTML
document.body.insertBefore(EL, document.body.firstChild)
}
poll()
</script>
</head>
<body
onload="window.ONLOAD = +new Date; somethingHappened('body[onload] ' + (window.READY ? (window.ONLOAD - window.READY) + 'ms after Ready' : ''), function(){return !!window.READY})"
onDOMContentLoaded="window.READY = +new Date; somethingHappened('DOMContentLoaded body[onDOMContentLoaded]', isNotLoaded)"
onReadyStateChange="somethingHappened('body[onReadyStateChange]')"
>
<div>
<div id=thingsthathappened></div>
<hr>
<script> somethingHappened('<i>Before serverSide flush/sleep</i> <hr>') </script>
<?php flushPause(1.0); ?>
<script> somethingHappened('<i>After serverSide flush/sleep</i> <hr>') </script>
<small>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</small>
<small>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</small>
<img height=16 width=16 onload="window.IMG_ONLOAD = true; somethingHappened('img[onload][src=about:blank]')" src="about:blank">
<img height=16 width=16 onload="window.IMG_ONLOAD = true; somethingHappened('img[onload][src=' + this.src + ']')" src="http://projects.subtlegradient.com/domready/big_image.jpg">
<script>document.write("<img height=16 width=16 onload=\"window.IMG_ONLOAD = true; window.IMG_ONLOAD_UNCACHED = true; somethingHappened('img[onload][src=' + this.src + ']')\" src=\"http://projects.subtlegradient.com/domready/big_image.jpg?_=" + +new Date + "\">")</script>
<!-- <script
onload="window.SCRIPT_ONLOAD = true; somethingHappened('script[onload][src=about:blank]')"
onreadystatechange="somethingHappened('script[src=about:blank][onreadystatechange]', this.readyState)"
onerror="somethingHappened('script[src=about:blank][onerror]')"
src="about:blank"></script> -->
<script
onload="window.SCRIPT_ONLOAD = true; somethingHappened('script[onload]')"
onreadystatechange="somethingHappened('script[onreadystatechange]', this.readyState)"
src="http://projects.subtlegradient.com/domready/blank.js"></script>
<script
onload="window.SCRIPT_ONLOAD = true; somethingHappened('script[onload][defer]')"
onreadystatechange="somethingHappened('script[defer][onreadystatechange]', this.readyState)"
defer src="http://projects.subtlegradient.com/domready/blank.js?123"></script>
</div>
<script>if (window != top) {somethingHappened('This is a frame!'); document.getElementsByTagName('html')[0].className += ' framed'} </script>
<?php
if (!isset($_GET['iframe'])){
$url = getCurrentURL();
$url .= (strpos($url, '?') !== false ? '&' : '?') . 'iframe';
echo '<iframe src="' . $url . '"></iframe>';
}
?>
<script>
somethingHappened('<i>All page content loaded and parsed! (Last <SCRIPT> on page)</i> <hr>')
window.PARSED = true
</script>
</body>
</html>
<?php flushPause(); ?>