faqts : Computers : Programming : Languages : JavaScript : DHTML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 5 people (80%) answered Yes
Recently 2 of 3 people (67%) answered Yes

Entry

How do I attatch an onclick event to an image in NS4, and will it still work in IE4+

Apr 29th, 2001 00:50
Colin Fraser, Mark Fritz,


Firstly, you need to have an event to capture and in most 
circumstances this would be a link. Try:
/*This element of the script preloads images into the cache on opening 
the document. */
	if (document.images) { 
		arrowPink = new Image 
		arrowBlue = new Image
                arrowRed  = new Image
		arrowPink.src="arrowpink.gif"
		arrowBlue.src="arrowblue.gif"
                arrowRed.src ="arrowred.gif"
	}  //end if
/*This function performs the actual rollovers in the document and 
appears AFTER the above code  */
function chgImage (imgName,newImg ) {
	if (document.images) {
		document[imgName].src=eval(newImg+".src")
	}  //end if
} //end function
In the loaded page 
<a href="newpage.html"
	onMouseOver="chgImage('pic0','arrowPink')"
        onClick = "chgImage('pic0','arrowRed')"
	onMouseOut="chgImage('pic0','arrowBlue')">
<img src="arrowblue.gif" border=0 name="pic0" alt="New Page"></a>
<p>
This code works in my pages, but no code is guarranteed. If you really 
want to use a link but dont want the user to actually go anywhere after 
they click, then you may want to replace the <a href=".."> to this: 
   <a href="javascript:void" ....>
This will work yet still allow you to utilize the events.