Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

OnMouseover display random image

Posted on 1997-07-09
1
Medium Priority
?
330 Views
Last Modified: 2011-10-03
Hi there,

I have a small JAVA applet running at
http://www.infonie.fr/public_html/germann/Index.htm     (please check),
the mouseover effect is used to display gif files at random.
Is it possible to do the same thing with JAVASCRIPT ?

(Note: the Java applet on my page works fine with Netscape3 but doesn't
work with N4)

Thanks,

Juan
germann@infonie.fr
0
Comment
Question by:Jgermann
1 Comment
 
LVL 5

Accepted Solution

by:
Trevor013097 earned 200 total points
ID: 1268397
Yes it is possible to do exactly the same thing in Javascript.First you must create an array of images which are preloaded and then a random function must pick one of these array entries at random (obviously!!) and then a simple onmouseover with an image displays the results.Here is how to do it:<HEAD>
<TITLE>Random Music</TITLE>
</HEAD>

<SCRIPT LANGUAGE="Javascript">

image1 = new Image();
image1.src = "image1.gif";
image2 = new Image();
image2.src = "image2.gif";
image3 = new Image();
image3.src = "image3.gif";
image4 = new Image();
image4.src = "image4.gif";

var imageArray = new makeArray('image1', 'image2', 'image3', 'image4');


function makeArray(){
this.length = makeArray.arguments.length
for (var i = 0; i < this.length; i++)
this[i + 1] = makeArray.arguments[i]
}

function randNum (num) {
var now = new Date();
var rand = Math.round(num * Math.cos(now.getTime()));
if (rand < 0) rand = - rand; if (rand == 0) rand++;
return rand;
}

var imgsrc = imageArray[randNum(imageArray.length)]

</SCRIPT>
</HEAD>
<BODY>


<A HREF="Your-page.html"
      onmouseover = "document.random.src = imgsrc"
      onmouseout = "document.random.src = imgsrc"
>
<IMG SRC="image1.gif" NAME=random BORDER=0></A>

</BODY>

</HTML>Hope this helps
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question