Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the window object that an HTML node belongs to using JavaScript?

Because of several iframes, XUL browser elements, and so forth, I have a number of window objects in my XULRunner application. I'm looking for the best way to find the window object that a specified node belongs to using JavaScript.

So, to be more specific, given node x, I need to find the specific window object that contains x.

like image 927
Joel Anair Avatar asked Oct 21 '08 23:10

Joel Anair


1 Answers

+1 to your question, it was exactly what I was looking for and thanks for the hint given directly by answering yourself.

I Googled a bit and according to http://www.quirksmode.org/dom/w3c_html.html cross-browsers tables I think the right answer is:

function GetOwnerWindow(html_node)
{
   /*
   ownerDocument is cross-browser, 
   but defaultView works on all browsers except Opera/IE that use parentWinow
   */
   return (html_node.ownerDocument.defaultView) ?
      html_node.ownerDocument.defaultView : 
      html_node.ownerDocument.parentWindow;
}

Or maybe even better:

return html_node.ownerDocument.defaultView || html_node.ownerDocument.parentWindow;

Plz let me know your thoughts.

like image 132
Marco Demaio Avatar answered Sep 21 '22 03:09

Marco Demaio