Image Swap

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sybil

    Image Swap

    Could someone tell me what is wrong with this function?
    function imageNext(){

    if ( document.part.s rc == "images/returnAdapter.g if"){
    document.part.s rc = "images/valveIntake.gif ";
    document.getEle mentById("c").s tyle.color = "black";
    document.getEle mentById("d").s tyle.color = "red";
    }

    The image name attribute is "part". But the image is not changing.

    Thanks, Sybil
  • Lasse Reichstein Nielsen

    #2
    Re: Image Swap

    sdw@bdgrp.com (Sybil) writes:
    [color=blue]
    > Could someone tell me what is wrong with this function?[/color]

    In which browser?
    [color=blue]
    > function imageNext(){[/color]
    [color=blue]
    > if ( document.part.s rc == "images/returnAdapter.g if"){[/color]

    The assumption that "document.p art" refers to the element with name
    "part", doesn't hold in all browsers. A safer way of referencing the
    element is
    document.images['part']

    You compare the value of the src property to a releative path.
    Most browsers (including IE6) changes to property to the absolute path.
    E.g., if I create an image as:
    <img src="../../PicA.png" id="foo">
    and then read the src value again with javascript (in IE 6)
    document.all.fo o.src
    the result is
    "file:///D:/Home/lrn/html/PicA.png"
    That means that your comparison will alway fail.
    You should test that the src *ends* with your string, e.g., with a
    regular expression:

    if (/images\/returnAdapter.g id$/.test(document. images['part'].src) {
    [color=blue]
    > document.part.s rc = "images/valveIntake.gif ";[/color]

    Again, "document.image sØ is safer.
    [color=blue]
    > document.getEle mentById("c").s tyle.color = "black";
    > document.getEle mentById("d").s tyle.color = "red";
    > }[/color]

    A "}" is missing here (just to be pedantic :)

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...