codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  the simplest dhtml wouldn`t work  mathfeel at 09:38 on Thursday, September 25, 2003
 

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!

  Re: the simplest dhtml wouldn`t work  Jeff Anderson at 11:00 on Thursday, September 25, 2003
 

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.

  Re: the simplest dhtml wouldn`t work  Troy Wolf at 20:54 on Thursday, September 25, 2003
 

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.
Troy Wolf: site expert
SnippetEdit Website Editor


  Re: the simplest dhtml wouldn`t work  mathfeel at 03:31 on Friday, September 26, 2003
 

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>
==========================================================================


  Re: the simplest dhtml wouldn`t work  Jeff Anderson at 07:43 on Friday, September 26, 2003
 

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!









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Cadenas.CoCreate.FTI.IMSI.CADCAM.DELCAM.GMI.PLAXIS
•  Cadenas.CoCreate.FTI.IMSI.CADCAM.DELCAM.GMI.PLAXIS
•  Schlumberger.PTC.Siemens.EDS.CATIA.CIMATRON.AUTODESK
•  PDMS.Plate.UGS.CYME.FLOMERICS.Tanner.LINUX.MAC.UNIX
•  Tanner.CrossLight.Elite.Software.Geocentrix.FLOW.3D.PTC.
•  ITTVIS.ENVI.Leica.Erdas.LEAP.SOFTWARE.MapInfo.Nobeltec.
•  Re: Cracked software ftp download 2013
•  Re: Crack Engineering Software
•  Full engineering software


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2013