jQuery better isWindow() is not for window

Same as me “jeresig” does not like to spend much time on some code just because some jokers, can produce a use-case which is breaking it. Where in the same time the same use-case is never seen and never used, in real application code situation.

One of the examples of this attitude is jQuery.isWindow(). It is rather simple (or “crude”) implementation and yes it is indeed rather easy to be a joker and break it.

This is the original jQuery function which John has decided not to improve.

Above is apparently “good enough” and jQuery leader and his team have decided to close comments on it’s API DOC page. And I can understand them being very tired of exchanges with over-excited kids on jQuery core forum.

But. If there is a simple and robust solution would it be possible to use it? I think I have one.

I see no reason for above not to work flawlessly anywhere where there is DOM, i.e. in any browser? Perhaps there is acceptable real-life use-case based on frames where this version of isWindow() breaks?

Actually thinking of which why not expanding a bit more on this useful idiom?

 

This tree little utilities should be a pretty firm confirmation (or denial) when checking the identity of three top level DOM objects. Even in the presence of frames, or jokers.

Update 14.Aug.11

Actually, why not being pure and true to oneself 🙂 In above utilities I have done a “cardinal sin”, I have produced a function which does “two things”. And not one but three of them. I was trying to find my quick way, around DOM issue of having frames. But that is not the same issue as checking if given object is a top level immediate global object. Instead why not having rock solid, consistent method of checking is object an immediate top level run-time environment object. Which in DOM happens to be “window”.

There you have it. A utility function which exhibits clear-cut responsibility, and works anywhere where there is JavaScript. DOM not necessary.

DOM is one of the very few, if not the only JavaScript run-time environment where there is a single global ‘this’
object available. Provided ES5 “use strict” is not used.

2 Replies to “jQuery better isWindow() is not for window”

  1. If you want to be consistent then window.top is not enough, you’d have to check all the other frames as well (window.frames). But maybe a more troubling question is: why is such a “crude” function even exposed publicly, without any notice?

    1. @Balazs
      I think You might believe me when I say that I know that I could check all the other frames. But this is where I think unreal use-case comes in, which is not worthy of solving?
      One another example might be deeply nested IFRAMES. Like hundreds of levels deep. In such a joke scenario dbj.isWindow() will certainly fail. But is that actually a real problem?

Comments are closed.