Using document vs element.ownerDocument

The dom elements have a field called ownerDocument. Given that there is already a document variable available in JavaScript, I thought that it was just a redundant piece of data available (though it’s part of the DOM specification). Then, today I realized a specific case where it was really useful. The use case was to open an IFRAME as a popup when the user clicks on a link. The IFRAME itself had to be embedded into a DIV element per my earlier article, Changing IFRAME content in IE. Now, the issue was, once the IFRAME was loaded, I didn’t want it to have scrollbar, so I had written a function to sync the IFRAME’s required size to the DIV element’s size. This works fine, but one issue remains. That is, sometimes the width of the IFRAME is larger than what can fit between the cursor and the right edge of the browser and I need to adjust it to move left. However, for this I need to access the document variable of the DIV from a JavaScript code that’s executed from within IFRAME’s webpage after it’s loaded. So, the way it can be done is, window.frameElement.parentNode.ownerDocument!


Filed under DHTML, javascript

2 responses to “Using document vs element.ownerDocument

  1. Vlad BALAN

    parent.document works too.

    But this is intersting too.

  2. Peiren

    thx, just tried, seems .parentNode is not necessary

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s