« Video: Steve Jobs and Bill Gates Interview on D 2007 | Main | Yahoo! Mail Offering Unlimited Storage »

Cross-browser Event Handler Assignment

Making the client side scripts work across different major browsers is not easy, especially when client debugging is not as convenient as more strongly typed programming languages.  Try the following web page.  It works fine on IE but on FireFox clicking the button will trigger nothing to happen.

<html xmlns="http://www.w3.org/1999/xhtml" >
<
head><title>Untitled Page</title></head>
<
script language="Javascript">
function
document.onclick() {
  alert(
'doc event');
}
function foo() {
  alert(
'foo');
}
</script>
<
body>
<
input type="button" value="ok" onclick="foo();" />
</
body>
</
html>

The page will silently fail and none of the event handlers works.  Change the document.onclick handler assignment into the following and it works on both browsers.  Tools like the Error Console on FireFox is very helpful in diagnosing these type of errors.

...
document.onclick = function () {
  alert(
'doc event');
}
...

TrackBack

TrackBack URL for this entry:
http://stanblog.jojoyao.com/cgi-bin/mt/mt-tb.cgi/49

Post a comment


About

This page contains a single entry from the blog posted on June 6, 2007 8:17 AM.

The previous post in this blog was Video: Steve Jobs and Bill Gates Interview on D 2007.

The next post in this blog is Yahoo! Mail Offering Unlimited Storage.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33