views:

91

answers:

1

Hello,

I am trying to figure out how to programmatically add an event handler to a JavaScript object. More specifically, I am trying to add an event handler to the onunload event of a window object. I am trying it like so with out any luck:

var url = "www.somelocation.com";
var specs = "location=0,menubar=0,status=0,titlebar=0,toolbar=0";

var dialogWindow = window.open(url, "dialog", specs, true);
dialogWindow.onunload += myEventHandler;

function myEventHandler(e)
{
  // Do stuff
}

I'm guessing my syntax is incorrect. However, I cannot seem to find any documentation regarding this. Can someone please help me?

Thanks,

+4  A: 

dialogWindow.onunload += myEventHandler is incorrect. This should really be:

dialogWindow.unload = myEventHandler;

Or to preserve the existing handlers:

var oldHandler = dialogWindow.unload;
dialogWindow.unload = function (e) 
{
   if (oldHandler) { oldHandler(e); }
   myEventHandler(e);
}

And, of course, use JQuery.

jsight