|
I have basically the following code in my header:
<script type="text/javascript">
<!--
function sm(){
alert("HAVE FUN!");
}
//-->
</script>
and the following in body:
<img src="/forum/image.jpg" style="width:200;height:150" onclick="sm()" />
I try the page on ie and when I click the image, there will be this code-error icon on the lower left, and when I d/c it, it says:
line: 73 (this is the line of the <img .../> tag)
char: 1
error: object expected
code: 0
Since this is (almost) the simplest possible code, I just don't know what's wrong and what to do with it...please help!
|
|
|
Hi mathfeel
The problem here is that you need to call the code not from the image tag but from an <a> tag surrounding the image. There are several alternatives and each method has its supporters, but all of these should work in a simple case like this:
<a href="javascript: void 0" onclick="sm()"><img src="/forum/image.jpg" style="width:200;height:150"></a>
|
|
<a href="#" onclick="sm()"><img src="/forum/image.jpg" style="width:200;height:150"></a>
|
|
<a href="javascript:;" onclick="sm()"><img src="/forum/image.jpg" style="width:200;height:150"></a>
|
|
Hope that helps!
Jeff
<Added>
My impression is that the last of these is most satisfactory - it's the one I see on all the big sites. But a Google search on javascript:; doesn't reveal much other than javascript sites, so I've been unable to confirm whether this is any kind of official method.
|
|
|
Hmmmmm....mathfeel, I looked at your code snippet and did not see anything wrong with it. Only thing that caught my eye was the use of the "/>" at the end of the image tag. I've never used that, and don't think I've seen it, but obviously it works. I always just use <img src=""> Anyway....
I copied and pasted your exact code snippet into a new HTML file. I did not change anything. It works perfect in IE6 and NS7. I don't see anything wrong. Neither browser produced any javascript errors. Both produced the javascript alert "HAVE FUN!" when I clicked the image.
Of course the code that Jeff posted works, too. But it is perfectly legal code to have an onclick event in an image tag.
|
|
|
|
|
Okay: This is my whole code...still error!!
============================================================================
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>TITLE</title>
<!-- Changed by: Matthew Zhang, 25-Sep-2003 -->
<meta http-equiv="Content-Type" content="text/html;" />
<script type="text/javascipt">
<!--
ie=document.getElementById;
ns6=document.all;
ns4=document.layers;
Menu_on=0;
function sm(){
alert("WHY IS THIS STILL NOT WORKING");
}
//-->
</script>
</head>
<body>
<img src="/forum/matthew01s.jpg" style="width:200;height:160;overflow:hidden" onclick="sm()" />
</body>
</html>
==========================================================================
|
|
|
get ready to kick yourself -
<script type="text/javascipt">
you put javascipt instead of javascript!!
Troy, thanks for the correction. I've never put the onclick in the image tag before, but I tried it and sure enough, it worked just the same!
|
|
|
|
|
|
|
|