How to show image before executing javascript?

Image switching in Javascript?

  • So I've seen a few websites where the viewer is given the option to change the colors of the layout by selecting a different option from a menu, clicking a button, etc. Is it possible to do the same thing with images? I'm not talking about making a slide show or some randomly placed image change on the page, but the graphics for the site itself. For my site I have a header image (for example) called Green.jpg and a different image called Blue.jpg. They are exactly the same size and the only difference is that one has a green tint to it and the other has a blue tint. If for say someone was to open a dropdown menu and select "Blue" and the header image changed from the green one to the blue one, how would I write that out in code? I'm not even sure if I can do this in Javascript but for what I am trying to do it seems like it should be within this field.

  • Answer:

    Yes this can be done with JavaScript, this is not a very difficult thing to do. Im just going to paste the code so you can copy and paste the code: <?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-… <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>slide show</title> <script language="JavaScript"> <!-- //first load the images var image1=new Image() image1.src="images/mario.jpg" var image2=new Image() image2.src="images/marioracing.jpg" //you can add more images if you like //rest of code function changeimage($step){ //if browser does not support the image object, exit. if (!document.images) return document.images.slide.src=eval("image"+ $step +".src") } // --> </script> </head> <body > <img src="images/mario.jpg" name="slide" width="100" height="56" /> <select name="select" onchange="changeimage(this.value)"> <option value="1" selected="selected">Mario</option> <option value="2">Marioracing</option> </select> </body> </html>

Domon at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

The key to pulling this switch is understanding the way JavaScript latches on to native HTML script tags. Recall from the tutorial that in object programming, a property is a characteristic of an object. These properties are initially defined in an HTML tag. For example, the image tag for the half-sized Web Developer® logo used in this example is: <IMG src="logo50.gif" width="177" height="52"> Fortunately for the task at hand, JavaScript lets us change the value of src. Because there may be many image tags in a given page, we need a way to refer to one specific tag. We do this by assigning a unique name to this tag with the name keyword: <IMG name="ImageOnly" src="logo50.gif" width="177" height="52"> Now we can use the identifier ImageOnly elsewhere on the page to refer to this particular image. Note: The name keyword is used frequently in JavaScript, and can be used to assign identifiers to such objects as forms, documents, frames, and so on. To change the value of a property, separate the name from the property you wish to change with a period. Thus, to change the logo in this example we'd use ImageOnly.src='js.gif'

Kiera

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.