Forum Moderators: open

Message Too Old, No Replies

changing image hover src with array information

doing tooltip-type images with getElementByTagName and id or class info

         

mincklerstraat

12:13 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a script which presents info in images when hovered over various keywords. It used to use css, switching on hover from relatively positioned / display none to absolutely positioned / display block; however, firefox now loads up all images without first hovering over them, and this is something that I'd like to avoid, since the server has to actually generate these images the first time they are called up (and caches them), so if you hit a page with 100 new keywords, it has to do a whole lot of work all at once. The images will be created more bit-by-bit if only called up and created when the user hovers over them.

The info can be provided in pretty much any way in the html - I prefer class, since id's are supposed to be unique, and the same keyword can appear more than once on a page.

My first attempt was to create an array of images within the specified div by using getElementByTagName, and then trying to src the image with a given uri string appended with the class name of that image. This gave me an error of the type 'missing name after . operator'.
I switched to using id instead of class, and no longer get this message, but the image does not swap on mouseover. This is what I currently have in the head:
<script>
function docpix()
{
if(document.getElementByTagName)
var x = document.thecode.getElementByTagName('img');
else return;
for (var i=0;i<x.length;$i++)
{

x[i].onmouseover = function () {
var im = new Image;
im.src = '/docbox.php?m=manual&l=' + x[i].id;
this.src = im.src;
}
x[i].onmouseout = function () {im.src= 'i/tr.gif';
this.src = im.src;}
}
}
</script>
images in the html body are like this:
<img src="i/in.gif" id="defined" />
This tag would be meant to grab /docbox.php?m=manual&l=defined in the hover state.
The function is called with:
<body onload=docpix> in the main body tag.

The version which registered the error was the same, except with x[i].class instead of x[i].id , and a class name in the images instead of an id name. Any tips or thoughts on the matter much appreciated. Once I get this down I'll worry about the css details of getting it to position properly as a tooltip, this isn't necessary at the moment.

I suppose I could also just use the old onmouseover / onmouseout in the links themselves, but since these keywords can occur a few hundred times in one document, it'd be great to save the bandwidth and do it with the replacement script.

mincklerstraat

1:44 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



solved, involved amongst other things calling the function at the bottom of the page instead of in <body onload> in order to get my document.writeln debugging messages visible in the html, getting the $i mistake out of the for loop, and using this.className instead of x[i].id .