Friday, 16 November 2007

 

Javascript, MouseOvers and Links



Yesterday I spent a good many hours working on changes to a client's website that had to be completed by this morning. What should have been only a couple of hours of work ended up actually being 14 hours of work. The thing that took the longest ended up being a series of Javascript mouseovers and links. The reason for this is that at the time they were done, they were originally not meant to be links, just mouseovers.

So I had used a Javascript preloader to load up the images and also provide the mouseover changes:

<script type="text/javascript">
<!-- Hide script from old browsers

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// End hiding script from old browsers -->
</script>

Along with the body tags to go with it:

<body onLoad="MM_preloadImages('../images/image1.jpg','../images/image2.jpg')">

Except we have 8 mouseovers, so there are a fair few more images in the preloader.

Now I had been using the following code where the images were located in order to perform the mouseover function:


<a href="javascript:;" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('image1','','images/image2.jpg',1)"><img src="images/image1.jpg" border="0" name="image1" />


This was working great, but when it came to adding a link it became difficult. I didn't want to edit the main script as it is used by other pages for mouseovers without links, so I needed to do something inline. I spent a couple of hours searching and trying different alternatives for this, but nothing I found seemed to want to work correctly. A few things I got to work in just Internet Explorer, but not in any other browsers, I got some to work with the link but lost the mouseover, I had a few solutions that opened a new window, but wouldn't open the page they were meant to and so on. It proved to be far more difficult than placing it in a separate script, and looking back it may have been a better option to create an additional script in addition to the main one, but I hadn't thought of that at the time.

I finally came across a solution that seems to have worked:

<a href="javascript:;" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(image1','','images/image2.jpg',1)" onClick="javascript:window.open('http://www.website.com')"><img src="images/image1.jpg" border="0" name="image1" /></a>


Once I worked this out, the simplicity of it amazed me, yet every possibility I tried seemed to be equally simple, they just didn't work correctly.

I hope this is useful to someone as I know that I had trouble finding a solution to this with a quick search.

Good luck and enjoy.

For anyone interested in the sites that I looked at for information on doing this they are as follows in no particular order:

Labels: , ,