-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
59 lines (48 loc) · 1.44 KB
/
index.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
/**
* Module dependencies.
*/
var getDocument = require('get-document');
/**
* Module exports.
*/
module.exports = getWindow;
var needsIEFallback = require('./needs-ie-fallback');
/**
* Returns `true` if `w` is a Window object, or `false` otherwise.
*
* @param {Mixed} w - Window object, maybe
* @return {Boolean}
* @private
*/
function isWindow (w) {
return w && w.window === w;
}
/**
* Returns the `window` object associated with the given `node`, which may be
* a DOM element, the Window object, a Selection, a Range. Basically any DOM
* object that references the Window in some way, this function will find it.
*
* @param {Mixed} node - DOM node, selection, or range in which to find the `window` object
* @return {Window} the `window` object associated with `node`
* @public
*/
function getWindow(node) {
if (isWindow(node)) {
return node;
}
var doc = getDocument(node);
if (needsIEFallback) {
// In IE 6-8, only the variable 'window' can be used to connect events (others
// may be only copies).
doc.parentWindow.execScript('document._parentWindow = window;', 'Javascript');
var win = doc._parentWindow;
// to prevent memory leak, unset it after use
// another possibility is to add an onUnload handler,
// (which seems overkill to @liucougar)
doc._parentWindow = null;
return win;
} else {
// standards-compliant and newer IE
return doc.defaultView || doc.parentWindow;
}
}