Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

While I'm aware of various different means different means of checking if a variable is a Window object in JavaScript, I'm interested in writing a function that is more accurate and resilient.

While I'm aware of various different means of checking if a variable is a Window object in JavaScript, I'm interested in writing a function that is more accurate and resilient.

While I'm aware of various different means of checking if a variable is a Window object in JavaScript, I'm interested in writing a function that is more accurate and resilient.

Tweeted twitter.com/#!/StackCodeReview/status/184469841036591104
added 107 characters in body
Source Link
zzzzBov
  • 526
  • 3
  • 15
console.assert(isWindow(window), '"window" is a window');
var o = {};
o.self = o;
o.window = o;
console.assert(!isWindow(o), '"o" is not a window');
var w = window.open('about:blank');
console.assert(isWindow(w), '"w" is a window');
var x = window.open('http://www.example.com');
console.assert(isWindow(x), '"x" is a window');
console.assert(isWindow(window), '"window" is a window');
var o = {};
o.self = o;
o.window = o;
console.assert(!isWindow(o), '"o" is not a window');
var w = window.open('about:blank');
console.assert(isWindow(w), '"w" is a window');
console.assert(isWindow(window), '"window" is a window');
var o = {};
o.self = o;
o.window = o;
console.assert(!isWindow(o), '"o" is not a window');
var w = window.open('about:blank');
console.assert(isWindow(w), '"w" is a window');
var x = window.open('http://www.example.com');
console.assert(isWindow(x), '"x" is a window');
added 310 characters in body
Source Link
zzzzBov
  • 526
  • 3
  • 15

While I'm aware of various different means of checking if a variable is a windowWindow object in JavaScript, I'm interested in writing a function that is more accurate and resilient.

(function (w) {
 "use strict";
 var wStr;
 wStr = Object.prototype.toString.call(w);
 if (!w.isWindow) {
 w.isWindow = function (arg) {
 var e,
 str,
 self,
 hasSelf;
 
 //Safari returns `DOMWindow`
 //Chrome returns `global`
 //Firefox, Opera & IE9 return `Window`
 str = Object.prototype.toString.call(arg);
 switch (wStr) {
 case '[object DOMWindow]':
 case '[object Window]':
 case '[object global]':
 return str === wStr;
 }
 if ('self' in arg) {
 //`'self' in arg` is true if `'self'` is in `arg` or a prototype of `arg`
 hasSelf = arg.hasOwnProperty('self');
 //`hasSelf` is true only if `'self'` is in `arg`
 try {
 if (hasSelf) {
 self = arg.self;
 }
 delete arg.self;
 if (hasSelf) {
 arg.self = self;
 }
 } catch (e) {
 //IE 7&8 throw an error when `window.self` is deleted
 return true;
 }
 }
 return false;
 };
 }
}(window));

A few assertions:

console.assert(isWindow(window), '"window" is a window');
var o = {};
o.self = o;
o.window = o;
console.assert(!isWindow(o), '"o" is not a window');
var w = window.open('about:blank');
console.assert(isWindow(w), '"w" is a window');

While I'm aware of various different means of checking if a variable is a window object in JavaScript, I'm interested in writing a function that is more accurate and resilient.

(function (w) {
 "use strict";
 var wStr;
 wStr = Object.prototype.toString.call(w);
 if (!w.isWindow) {
 w.isWindow = function (arg) {
 var e,
 str,
 self,
 hasSelf;
 
 //Safari returns `DOMWindow`
 //Chrome returns `global`
 //Firefox, Opera & IE9 return `Window`
 str = Object.prototype.toString.call(arg);
 switch (wStr) {
 case '[object DOMWindow]':
 case '[object Window]':
 case '[object global]':
 return str === wStr;
 }
 if ('self' in arg) {
 //`'self' in arg` is true if `'self'` is in `arg` or a prototype of `arg`
 hasSelf = arg.hasOwnProperty('self');
 //`hasSelf` is true only if `'self'` is in `arg`
 try {
 if (hasSelf) {
 self = arg.self;
 }
 delete arg.self;
 if (hasSelf) {
 arg.self = self;
 }
 } catch (e) {
 //IE 7&8 throw an error when `window.self` is deleted
 return true;
 }
 }
 return false;
 };
 }
}(window));

While I'm aware of various different means of checking if a variable is a Window object in JavaScript, I'm interested in writing a function that is more accurate and resilient.

(function (w) {
 "use strict";
 var wStr;
 wStr = Object.prototype.toString.call(w);
 if (!w.isWindow) {
 w.isWindow = function (arg) {
 var e,
 str,
 self,
 hasSelf;
 
 //Safari returns `DOMWindow`
 //Chrome returns `global`
 //Firefox, Opera & IE9 return `Window`
 str = Object.prototype.toString.call(arg);
 switch (wStr) {
 case '[object DOMWindow]':
 case '[object Window]':
 case '[object global]':
 return str === wStr;
 }
 if ('self' in arg) {
 //`'self' in arg` is true if `'self'` is in `arg` or a prototype of `arg`
 hasSelf = arg.hasOwnProperty('self');
 //`hasSelf` is true only if `'self'` is in `arg`
 try {
 if (hasSelf) {
 self = arg.self;
 }
 delete arg.self;
 if (hasSelf) {
 arg.self = self;
 }
 } catch (e) {
 //IE 7&8 throw an error when `window.self` is deleted
 return true;
 }
 }
 return false;
 };
 }
}(window));

A few assertions:

console.assert(isWindow(window), '"window" is a window');
var o = {};
o.self = o;
o.window = o;
console.assert(!isWindow(o), '"o" is not a window');
var w = window.open('about:blank');
console.assert(isWindow(w), '"w" is a window');
added 30 characters in body
Source Link
zzzzBov
  • 526
  • 3
  • 15
Loading
Source Link
zzzzBov
  • 526
  • 3
  • 15
Loading
default

AltStyle によって変換されたページ (->オリジナル) /