Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get reference of window object from a dom element

I have a javascript code like this

var element = $("elementId"); 

I got the reference to the element (which is a div).

Now I need to get the reference to the window in which this div element resides. But the problem is, here the $ is passed from a different window. So now the element resides in a different window.

How to get reference to that window object which contains this div element? Pls Help.

like image 214
Aniket Avatar asked Apr 15 '13 07:04

Aniket


People also ask

How do I access a Windows object?

Opening Browser Windows using JavaScript A new browser window can be opened from a JavaScript script using the open() method of the window object. The syntax for opening a new window is as follows: newWindowObj = window.

Is window object part of DOM?

The window object is not part of the DOM. It is a host object implemented as the "global object" to complete an ECMAScript implementation. It has its own standard which is available from the W3C.

What is window object in DOM?

The window object is the topmost object of DOM hierarchy. It represents a browser window or frame that displays the contents of the webpage. Whenever a window appears on the screen to display the contents of document, the window object is created.


1 Answers

Get a reference to the DOM node, use the ownerDocument property to get a reference to the document, then read its defaultView property (parentWindow for IE8-) to get a reference to the window:

var $element = $('#elementId'); var element = $element[0]; // Assume that element exists, otherwise an error will be thrown at the next line var doc = element.ownerDocument; var win = doc.defaultView || doc.parentWindow; 
like image 165
Rob W Avatar answered Oct 24 '22 05:10

Rob W