JScript Modifying Events at Runtime

I have two web pages. The main page contains several VML rectangles that have their "onclick" events set to javascript code. The second page is opened by the first page when a particular link is clicked. What I want to do is change the onclick event of a <v:rect> element on the main page from the 2nd page.

So far I am able to retrieve the reference to the object and view it's initial onclick event:

// this code is in the 2nd page that was opened by the main page
var rectelem = window.opener.document.getElementById('event2_2372');
alert(rectelem.onclick); // this works and shows me what it is initially set to.
rectelem.onclick = 'alert(\'This is the new onclick event!\');' ;
alert(rectelem.onclick); // shows that the change was made.

When I switch back to the main page and click on the object though, the initial onclick behavior is gone but the new behavior does not take place. Nothing happens and I get no script error to look at. I've tried moving the new onclick behavior to a function and then setting the object's onclick to the address of that function:

rectelem.onclick = test;

function test()
{
alert('This is the test function!');
}

This approach has had the same result so far :( Any ideas on what I'm doing wrong or how to change this events behavior at runtime?
[1436 byte] By [TSmooth] at [2007-11-20 2:13:54]
# 1 Re: JScript Modifying Events at Runtime
I have since been able to get the second approach to work by putting the function test() on the main page and then setting the onclick event = to window.opener.test. This still leaves me with the underlying problem though which is that I need to change what test() does during runtime which I'm not sure how to do.
TSmooth at 2007-11-8 0:41:12 >
# 2 Re: JScript Modifying Events at Runtime
I came up with the following solution:

On the main page I have a function that takes a string and then executes that string as code using the eval function:

function execCode(codestring)
{
eval(codestring);
}

Then on the popup page I do the following to change the onclick event's code:

window.opener.execCode("window.document.getElementById('event2_2372').onclick = function() { alert('Modified event code!'); };");

This allows the "function()" assignment to take place in the address space of the main page thus giving it a valid entry point to the function.

If anyone has any tweaks or a better way to do this, please let me know. Thanks!
TSmooth at 2007-11-8 0:42:15 >