views:

44

answers:

3

I already have a set of functions I use to extend the document (i.e. bind/unbind). So I can do stuff like: document.bind('load',someAction,{})

But it doesn't work if I do: $('some_iframe').contentWindow.document.bind(...) And apparently, $('some_iframe').contentWindow.document.prototype doesn't exist.

EDIT: Here is the code breakdown:

//the eggplant library
eggp = {
    extend: function(dest, source){
        for(var prop in source)
            dest.prototype[prop] = source[prop];
        return dest;
    },
    //other functions below...  
    bind{},  
    unbind{}
}
//extend the DOM
eggp.extend(Document, eggp);
//extending the iframe document DOESN'T WORK
eggp.extend(someiframe.contentWindow.document, eggp);

I've checked to see if someiframe.contentWindow.document is undefined, but it returns object HTMLDocument

A: 

How are you extending document initially? Don't you have to extend HTMLDocument.prototype instead of document.prototype, since the latter isn't a constructor?

Meaning you're extending the wrong thing in the iframe? If I'm wrong, please tell me how you are extending it initially.

meder
My extend function takes a reference of the destination and source. Then loops through all the sources functions and extends the destination. So it goes: dest.prototype[prop] = src[prop], emulating what I believe prototype.js does. So, to extend the document with my library of functions I say: library.extend(Document,library); and it works for doing document.bind(...) but not for iframe.contentWindow.document.So, would should I do HTMLDocument instead?
Azmisov
I checked with HTMLDocument, and it made no change.
Azmisov
A: 

I am guessing "some_iframe" is an ID. To select an object by ID with jQuery you need to use #, just like in CSS.

So it should be $('#some_iframe').eq(0).contentWindow.document.prototype.

If you don't use # that is used to select by TAG name.

So $('p') would select all <p> tags on the page.

Strelok
Not sure why the downvote. But I am pretty sure I am right in what I said.
Strelok
Has nothing to do with the question. (I didn't downvote you, for the record.)
musicfreak
I didn't downvote your answer, but I'm using my own library.
Azmisov
+1  A: 

You can't do this, at least not in a cross-browser way. Ideally you would extend the prototype of the HTMLDocument object, but of course, IE does not have this object in the first place. As a workaround, you could create a function that creates an IFrame and automatically extends its document object, and exclusively use that function to create your IFrames, but if the frames are already on the page, there isn't much you can do about it short of looping through each one and extending it manually.

$('iframe').each(function() {
    MyLibrary.extend(this.contentWindow.document, MyDocumentPrototype);
});

(Assuming jQuery, insert your own library code to get all IFrames here.)

In general, though, extending built-in DOM objects is a bad idea, so you should come up with another way to do what you want.

musicfreak
Yeah, I'm beginning to realize the problems with extending the DOM, but I think I'll continue working with it until I've learned enough that I can make a better interface. Anyways...In this code, similar to your example, using my library:`eggp.extend(someiframe.contentWindow.document, eggp);`will return `dest.prototype is undefined`
Azmisov
@Azmisov: `document` does not have a `prototype` object. You have to extend it directly: `document.property1 = "blah"` etc.
musicfreak
Thanks, that's all I needed to know.
Azmisov