nxgn
05-27-2009, 03:49 PM
I'm a JS noob and I've been at this for days... (trying to set up external event handlers)
When I register an event handler directly into the HTML tag everything works fine, but I'm trying to register them from the external JS file where the function is, and that doesn't work at all. I've read that this is called the "traditional method" and that it should work.
I've tried it a million different ways, but what I'm putting below AFAIK is correct... but it just doesn't work. I've tried it in Firefox, IE, and Chrome - and used the "Inspect element" feature in Chrome, and am not getting any error messages.
Here is just a simple example of what I'm trying to do - its extremely basic, I know, but I just can't figure out why it won't work.
HTML:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>SITE</title>
<link rel="stylesheet" type="text/css"
href="sitecss.css" />
<script type="text/javascript" src="sitejs.js"></script>
</head>
<body>
<h1 id="nav">Logo</h1>
<p id="main">C'mon work!</p>
</body>
</html>
JS:
function message(){
alert('Hello');
}
foo="document.getElementById('nav')";
foo.onclick=message;
-------------------------------------------------
(Again, it works perfectly when I stick the onclick="message()" event handler directly into the HTML h1 tag, but not when in the external file.)
I'd really appreciate any help/suggestions. I've been at this for way too long and it's driving me nuts.:eek:
When I register an event handler directly into the HTML tag everything works fine, but I'm trying to register them from the external JS file where the function is, and that doesn't work at all. I've read that this is called the "traditional method" and that it should work.
I've tried it a million different ways, but what I'm putting below AFAIK is correct... but it just doesn't work. I've tried it in Firefox, IE, and Chrome - and used the "Inspect element" feature in Chrome, and am not getting any error messages.
Here is just a simple example of what I'm trying to do - its extremely basic, I know, but I just can't figure out why it won't work.
HTML:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>SITE</title>
<link rel="stylesheet" type="text/css"
href="sitecss.css" />
<script type="text/javascript" src="sitejs.js"></script>
</head>
<body>
<h1 id="nav">Logo</h1>
<p id="main">C'mon work!</p>
</body>
</html>
JS:
function message(){
alert('Hello');
}
foo="document.getElementById('nav')";
foo.onclick=message;
-------------------------------------------------
(Again, it works perfectly when I stick the onclick="message()" event handler directly into the HTML h1 tag, but not when in the external file.)
I'd really appreciate any help/suggestions. I've been at this for way too long and it's driving me nuts.:eek: