Forum Moderators: open
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.
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!
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
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...
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.