UNPKG

get-window

Version:

Returns the `window` object from a DOM object

60 lines (48 loc) 1.47 kB
/** * 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; } }