-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrowser.js
73 lines (61 loc) · 2.1 KB
/
browser.js
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
'use strict';
var readystate = module.exports = require('./readystate')
, win = (new Function('return this'))()
, complete = 'complete'
, root = true
, doc = win ? win.document : null
, html = doc ? doc.documentElement : null
, docReadyState = function () { return doc ? doc.readyState : null; };
(function wrapper() {
//
// Bail out early if the document is already fully loaded. This means that this
// script is loaded after the onload event.
//
if (complete === docReadyState()) {
return readystate.change(complete);
}
//
// Use feature detection to see what kind of browser environment we're dealing
// with. Old versions of Internet Explorer do not support the addEventListener
// interface so we can also safely assume that we need to fall back to polling.
//
var modern = doc && !!doc.addEventListener
, prefix = modern ? '' : 'on'
, on = modern ? 'addEventListener' : 'attachEvent'
, off = modern ? 'removeEventListener' : 'detachEvent'
, doScroll = html ? html.doScroll : null;
if (!modern && 'function' === typeof doScroll) {
try { root = !win.frameElement; }
catch (e) {}
if (root) (function polling() {
try { doScroll('left'); }
catch (e) { return setTimeout(polling, 50); }
readystate.change('interactive');
}());
}
/**
* Handle the various of event listener calls.
*
* @param {Event} evt Simple DOM event.
* @api private
*/
function change(evt) {
evt = evt || win.event;
if ('readystatechange' === evt.type) {
readystate.change(docReadyState());
if (complete !== docReadyState()) return;
}
if ('load' === evt.type) readystate.change('complete');
else readystate.change('interactive');
//
// House keeping, remove our assigned event listeners.
//
(evt.type === 'load' ? win : doc)[off](evt.type, change, false);
}
//
// Assign a shit load of event listeners so we can update our internal state.
//
doc[on](prefix +'DOMContentLoaded', change, false);
doc[on](prefix +'readystatechange', change, false);
win[on](prefix +'load', change, false);
} ());