Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Event Handlers

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?

like image 913
Villager Avatar asked Mar 01 '23 11:03

Villager


1 Answers

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.

like image 105
jsight Avatar answered Mar 11 '23 16:03

jsight