tags:

views:

259

answers:

1

I open a new window using the following code:

purchaseWin = window.open("Purchase.aspx","purchaseWin2", "location=0,status=0,scrollbars=0,width=700,height=400");

I want to access the dom tree of the purchaseWin, e.g.

purchaseWin.document.getElementById("tdProduct").innerHTML = "2";

It doesn't work. I can only do this:

purchaseWin.document.write("abc");

I also try this and it doesn't work too:

 $(purchaseWin.document).ready(function(){

     purchaseWin.$("#tdProduct").html("2");

   });

What should I do?

+1  A: 

With jQuery, you have to access the contents of the document of your child window:

$(purchaseWin.document).ready(function () {
  $(purchaseWin.document).contents().find('#tdProduct').html('2');
});

Without libraries, with plain JavaScript, you can do it this way:

purchaseWin.onload = function () {
  purchaseWin.document.getElementById('tdProduct').innerHTML = '2';
};

I think that the problem was that you were trying to retrieve the DOM element before the child window actually loaded.

CMS
The plain Javascript version works.But jQuery version fails. I need to run the jQuery code manually in parent window to work.
Billy
Works in IE (not firefox): $(purchaseWin.document).ready(function () {$(purchaseWin.document).contents().find('#tdProduct').html('2');});Works in FF (not IE): purchaseWin.onload = function () {$(purchaseWin.document).contents().find('#tdProduct').html('2');};
Billy