only executes in child window

M

mike

I have a parent window where I open a child window.

In that child window I have some js that creates a row in the parent
window, it looks like:

if ( self.opener.document.update )
{
var actTable =
self.opener.document.getElementById("Action_Table");
myTR = self.opener.document.createElement("TR");
myTD = self.opener.document.createElement("TD"); myA =
self.opener.document.createElement("A"); myA.href =
"javascript:void(null);"; myA.onclick = function (evt) {
alert('mike'); } myA.className = "greenlink";
myTEXT =
self.opener.document.createTextNode('click'); myA.appendChild(myTEXT);
myTD.appendChild(myA);
myTR.appendChild(myTD);
var lastRow = actTable.rows[actTable.rows.length-1];
lastRow.parentNode.appendChild(myTR);
}

This js creates the row in the parent window ok, but I only get the
alert message when the child window is up. After I close the child
window I get no alert.

Mike
 
M

mike

I was thinking that:

myA.onclick = function (evt) { alert('mike'); }

was associated with the tag now in the parent window.

So, If I wanted to do this correctly, how would it be done then?

Mike
 
M

mike

I have a parent window with some "parent data" and it contains the
"keys" to child data.

Consider the following root data:
parent_id title
1 first
2 second
3 third

Consider the following child data:
parent_id child_id child_title
1 1 first action
1 2 second action
2 1 another first action
2 2 another second action

The user updates the root data and sometimes need to create a new
child. So the functionality exists to open a new window and when the
new window is opened a new child is created in the db. The javascript
in the child window creates a new row in the parent window with a
"link", i.e. the onclick function we are talking about. That link
contains the parent_id and child_id so it can be updated again if the
user needs to.

The alternative would be to "refresh" the parent window after the child
is created, but that takes time and it is faster to just create the row
in the parent window dynamically, with a link in case the user needs to
re-update the child data again.

What you are saying is the function assocaited with the onclick event
remains in the child window even though I can see the link in the
parent window.

I hope that makes some sence. Appreciate your help.

Mike
 
M

mike

Well I just placed the code in the parent window and made a call to it
from the child using:

self.opener.mynewfunction(par_id,chd_id);

works fine ...
 
R

Richard Cornford

mike said:
I was thinking that:

myA.onclick = function (evt) { alert('mike'); }

was associated with the tag now in the parent window.

In javascript functions are objects, and evaluating a function
expression causes a function object to be created and a reference to
that function to be the result of the expression (it is the reference to
the (anonymous) function object that is assigned to the event handler).
That function object must reside somewhere and as it was created by code
in the pop-up it is likely to reside in memory associated with the
pop-up, and so became unavailable when the pop-up closed.
So, If I wanted to do this correctly, how would it be
done then?

There is no correct way of doing this. ECMA 262 assumes a single
execution environment, but multiple windows/frames represent multiple
execution environments, and the spec has nothing to say about how they
should interact.

What you want to do is arrange that the function object that is created
and assigned as the event handler is created in the opener rather than
in the pop-up. You should be able to do this by creating a function in
the opener that crates the function you want locally and returns a
reference to that function to the pop-up code.

I could show you how to do that in several ways but I observe that you
are already crating elements with -
self.opener.document.createElement("TD"); - so maybe all of the table
crating code should be moved into a function in the opener and just
called from the pop-up.

Pop-ups are of course terribly unreliable theses days so it probably
isn't sensible to be going in this direction at all, as the result will
be a system that is needlessly unreliable by design.

Richard.
 
M

mike

Crap ...

I hated when you said this:
Pop-ups are of course terribly unreliable theses days so it probably
isn't sensible to be going in this direction at all, as the result will
be a system that is needlessly unreliable by design.

I am experiencing some inconsistency. See the new item "IE has to
close".

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top