Document.body.onclick - Why Would This Not Work?

Now here’s something that I thought was basic and simple to do…

<script type="text/javascript">
	document.body.onclick = alert('test');
</script>

[FONT=“Georgia”]…I put that just before my close body tag, thinking that from now an anytime I clicked on my page I’d see an alert saying “test”.

What actually happens, though, is when the page loads the alert pops up and nothing happens afterward.

The clicks are just ignored.

Also there are no javascript errors coming up so I don’t know what’s the cause of the problem.

Why wouldn’t this work ??

[/FONT]

try:

<script language=“javascript”>

window.onClick= function()
{
alert(‘test’);
}

</script>

You misspelt both type=“text/javascript” (there is no language attribute any more) and onclick which must be spelt with a small C as JavaScript is case sensitive.

Try:


<script type="text/javascript">
window.onclick = function() {alert('test');}
</script>

[FONT=“Georgia”]Okay.

Thanks that worked. :tup:

I figured the alert(); being a function itself wouldn’t need to be in the function(){} syntax but I guess not.

[/FONT]