0

This answer https://stackoverflow.com/a/4196018/545430 states that the "this" object when used within a JQuery callback will refer to the selected element to which the callback is being added. However this doesn't appear to work when setting the ready callback of a child window:

var new_window = window.open('./NewWindow.aspx', '_blank');
$(new_window.document).ready(function () {
    console.dir(this);
});

In the above example this is clearly the document of the parent window and not the "NewWindow" document.

Community
  • 1
  • 1
Ian
  • 3,577
  • 2
  • 30
  • 52

1 Answers1

2

The 'ready' event is happening in the document https://learn.jquery.com/using-jquery-core/document-ready/

if you want to override this with something different inside your event handler you have to bind different context:

var new_window = window.open('./NewWindow.aspx', '_blank');
$(new_window.document).ready(function () {
    console.dir(this);
}.bind(new_window));

More details https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind

Andrei Zhytkevich
  • 7,419
  • 2
  • 27
  • 43