...

View Full Version : cross-browser events API



kae
03-16-2005, 08:49 PM
hi all,
I'm completely new to this forum, so if this question has been asked a thousand times before, please simply post a URL. thanks :)

now, my company's bosses have been becoming increasingly frustrated at me recently, as I tend to code for Firefox first, and IE as an afterthought.

a lot of the code I have trouble with is events-based, although there are some DOM-navigation problems that I've noticed in IE.

before I launch into a probably fruitless attempt to write an API that corrects IE's DOM-mangling, I'm wondering if someone else may have already invented that particular wheel.

ideally, this would be a script which I simply link to in the head, and then write valid DOM code which automagically works.

Thanks,
Kae

glenngv
03-17-2005, 03:34 AM
These are not API for events but are definitely a good read.

http://www.quirksmode.org/js/introevents.html
http://www.quirksmode.org/js/events_compinfo.html
http://www.quirksmode.org/js/events_access.html

glenngv
03-17-2005, 04:00 AM
I found a cross-browser events API here (http://www.severnsolutions.co.uk/twblog/archive/2004/06/20/accessiblejavascripteventhandles)

kae
03-17-2005, 10:40 AM
Thanks Glenn.

Unfortunately, those links don't really answer the question I had, although they may help me if I do need to build an API myself.

What I was hoping for was something that would allow me to write code such as this:

<html>
<head>
<script type="text/javascript">
window.addEventListener('click',function(){alert('test');},false);
</script>
</head>
<body>
</body>
</html>

The above code will work in Firefox, but not IE.

I can make it work by first defining addEventListener for IE:

window.addEventListener=function(ev,fn){window.attachEvent('on'+ev,fn);}

unfortunately, that's a rather cumbersome way of doing it, as I would have to specifically fix every element in the document, and be aware that the fix may not apply to dynamically generated elements.

an ideal solution would be to apply the events fixes to the object prototypes, but IE seems to completely ignore inheritance, so there's no point doing that.

Kae

brothercake
03-17-2005, 12:02 PM
Have a look at my "encapsulated anonymous functions" thread - you might like that approach -- http://www.codingforums.com/showthread.php?t=47379

But what this question comes down to, as you're seeing, is that browsers vary hugely, and there is no unified model; maybe there would be if IE wasn't so outdated, but whatever ... it's as it is :)

The best approach to most scripting is to start from the assumption that everything works as it's supposed to, unless it's known not to; then hack from there until you've covered or excluded the browsers you care about. Effectively, write for the correct case (which is usually, but not always, what Firefox does), plus exceptions (IE builds, Safari, Opera ... as necessary).

kae
03-17-2005, 12:35 PM
ah! that's an interesting approach.

I think it will take a bit of practice for it to sink in for me, but it does look like that's what I need.

Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum