views:

567

answers:

2

I have a .aspx page that has a link on it, then when clicked opens a new window using window.open.

I need to send a integer back and put that number into a textbox (which is a .NET control).

When I call window.opener on the popuped up window, I have to reference the ID of the textbox. The issue is, the ID changes from time to time if you add things to the control tree.

How can I reliably reference the textbox's ID from the new window?

I have jQuery installed also, but not sure if I can use jQuery from the new window?

+1  A: 

This should work

// original window script
var windowHandle = window.open(...);
windowHandle.top.otherWindowTextBox = document.getElementById('idOfTextBox); // or use jQuery

Now in you popup window, you have a reference to your textbox on the page that opened the popup window.

// script in popup window.
top.otherWindowTextBox.value = someInteger;
nickyt
Note: You should also check that `top.otherWindowTextBox` is not undefined which might happen if the window that opened the popup closes.
nickyt
+2  A: 

Instead of accessing the element directly from the popup, put a function in the page that the popup can call. In the function you can insert the actual id of the element:

function setTextbox(value) {
   document.getElementById('<%=TheTextBox.ClientID%>').value = value;
}

In the popup:

window.opener.setTextbox("Hello world!");
Guffa