Forum Moderators: open

Message Too Old, No Replies

How to identify document and window objects

         

gappel

7:28 am on Jul 9, 2004 (gmt 0)

10+ Year Member



Hello Ladies and Gents,

Consider the following JS function:
function MyFunc(obj)
{
// do stuff based on the kind of object
}

The "obj" may be one of the following:
1) current window / some other frame (window)
2) top window
3) current document / some other frame's document
4) instanceof XMLHttpRequest
5) instanceof ActiveXObject
6) custom object that has nothing to do with anything

I am sure my question does not sound original but still...how the hell am I suppose to identify the type of object I am working with?

If I am not mistaken, instanceof and similar crap returns "Object" for window and document object types.

Note that I do not care about the "context" in which I am called i.e. I do the same things for top/current/other window objects for instance.

Thank you all very much,
Greg.

Bernard Marx

8:16 am on Jul 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) current window / some other frame (window)

obj == window // or
obj == self

2) top window

obj == top

3) current document / some other frame's document

That's trickier, as we are working within the host, not a particular document.
But, for a particular element we can get

elem.ownerDocument

4) instanceof XMLHttpRequest
5) instanceof ActiveXObject

I reckon

instanceof
will work there, else try

obj.constructor == XMLHttpRequest

6) custom object that has nothing to do with

Not sure, as everything's an instanceof Object, and if the actual object type is arbitrary, you can't test for the constructor. All I can think of right now is to test that it's not everything else!

gappel

10:07 am on Jul 9, 2004 (gmt 0)

10+ Year Member



Than**** :)

I didn't quite get you with the document stuff?
Could you elaborate?

Bernard Marx

10:34 am on Jul 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm. I wonder what accidental emphatic wasn't considered kosher this time?

It could be said that you're not working in a document, but in the host (window), and are holding references to elements in a document. Generally there is only one document (as in document.images), but you could have XML islands etc. If you have an element reference of some kind, and want to get a reference to the actual document that it's part of:

elementRef.ownerDocument

I suppose that could be tested against

window.document
, if you like

gappel

7:10 am on Jul 11, 2004 (gmt 0)

10+ Year Member



OK, but that's not good enough because as I said, I have no clue about the kind of object I recieve. That means I cannot make assumptions about it being an element - it might be the windo itself.
In addition, it might be a diferent frame's window or better yet, an IFRAME object...

What would you say about scanning the DOM tree.
I think it is a very promissing idea.
...The only thing I can rely on is that "this" points to a current window object.
I can start traversing the tree from there...and yes, I know it sounds inefficient...

Bernard Marx

10:09 am on Jul 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I cannot make assumptions about it being an element

I know. You will have to test & branch all the way through. That's the nature of the game. It's a complicated procedure.

var doc;
var win
var isMainWin
// is it a document element?
if(test.ownerDocument)
{
doc = test.ownerDocument
win = doc.parentWindow // IE only
isMainWin = win == window
}
else
{
// could be primitive, or native, host or custom object

}

IE5 doesn't support

element.ownerDocument
property. You'll have to use the
element.document
property instead (if you need to support IE5).

You can find the document's window (which frame etc) using

parentWindow
property in IE. I don't know how this is done in other browsers.

I'd suggest not using

this
.
this
doesn't always refer to the window. It is a reference to the context of the function call, so it may refer to another object. If your testing function isn't used as an object method, then this won't happen, but you might as well use
window
, as this is 100% explicit.