[thelist] [JavaScript] event location issue restated

Matt Warden mwarden at gmail.com
Wed Feb 22 11:00:51 CST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tom Dell'Aringa wrote:
> Let me restate my earlier question. I have a page on this application of some 40-50 pages that has
> inline event handler calls like onclick=func(). I prefer to do things unobtrusively.
> 
> So, I can add this line in my main js file:
> 
> addBodyOnload(myFunc);
> 
> And add the events at load time. The problem is, this code will execute on 40-50 pages. So is the
> tradeoff worth it to add it to the onload queue? Or does it make more sense to leave it inline...
> maintenance wise its better unobtrusive.

If you return from myFunc as soon as you realize the element(s) you're
trying to init are not on that page, then there really aren't many
cycles lost. e.g.:

function myFunc()
{
   var services = document.getElementById('cmb_services');
   if (!services) return;
   services.onchange = handleServiceChange;
   ...
}

For pages where this initialization isn't necessary, it would return on
line 2. However, you have to be very careful that your IDs are unique
application wide, now.

Personally, I move code like this into external javascript files and
include them only when necessary. Prereq/dependency files are the ones I
always include (like data structures, the event handling functions like
addBodyOnload, my ajax utility class, etc.).

- --
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFD/JjDrI3LObhzHRMRAtjHAKCpdiYFZAJCTK12G/rBj6DP2veL7wCfXqg5
3yZ9sTaUE4ZlkOuRpQ1MVow=
=VFXw
-----END PGP SIGNATURE-----



More information about the thelist mailing list