Help to Modify Script

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

    #1

    Help to Modify Script

    I really like this javascript for an image slideshow. The only thing I woudl
    like to do differently is have a cell above the image and navigation that
    would allow text content to be placed.

    Does anyone have an idea of how that might be acomplished?

    Header....

    <SCRIPT LANGUAGE="JavaS cript">
    <!-- Begin
    NewImg = new Array (
    "http://pic1.jpeg"
    ,"http://pic2.jpeg"
    ,"http://pic3.jpeg"
    ,"http://pic4.jpeg"
    );
    var ImgNum = 0;
    var ImgLength = NewImg.length - 1;

    //Time delay between Slides in milliseconds
    var delay = 3000;

    var lock = false;
    var run;
    function chgImg(directio n) {
    if (document.image s) {
    ImgNum = ImgNum + direction;
    if (ImgNum > ImgLength) {
    ImgNum = 0;
    }
    if (ImgNum < 0) {
    ImgNum = ImgLength;
    }
    document.slides how.src = NewImg[ImgNum];
    }
    }
    function auto() {
    if (lock == true) {
    lock = false;
    window.clearInt erval(run);
    }
    else if (lock == false) {
    lock = true;
    run = setInterval("ch gImg(1)", delay);
    }
    }
    // End -->
    </script>


    in body....

    <img src="pic1.jpeg" name="slideshow ">
    <table>
    <tr>
    <td align="right">< a href="javascrip t:chgImg(-1)">Previous</a></td>
    <td align="center"> <a href="javascrip t:auto()">Auto/Stop</a></td>
    <td align="left"><a href="javascrip t:chgImg(1)">Ne xt</a></td>
    </tr>
    </table>


  • Thomas 'PointedEars' Lahn

    #2
    Re: Help to Modify Script

    scorpion53061 wrote:[color=blue]
    > I really like this javascript for an image slideshow.[/color]

    No, you don't want that. It is badly written and error-prone.
    [color=blue]
    > The only thing I woudl like to do differently is have
    > a cell above the image and navigation that
    > would allow text content to be placed.
    >
    > Does anyone have an idea of how that might be acomplished?[/color]

    Make your array an array of objects, then use the
    usual DOM methods to change the text node's value
    according to the current object's property value.
    [color=blue]
    > Header....
    >
    > <SCRIPT LANGUAGE="JavaS cript">[/color]

    <script type="text/javascript">
    [color=blue]
    > <!-- Begin[/color]

    Nonsense, ask Google Groups.
    [color=blue]
    > NewImg = new Array ([/color]

    The call operator should not be separated by whitespace from the
    called function/method; this should be reserved for other operators
    and operands.
    [color=blue]
    > "http://pic1.jpeg"
    > ,"http://pic2.jpeg"
    > ,"http://pic3.jpeg"
    > ,"http://pic4.jpeg"
    > );[/color]

    Why, are relative URIs not good enough?
    [color=blue]
    > var ImgNum = 0;[/color]

    Only constructor identifiers should start uppercase.
    [color=blue]
    > var ImgLength = NewImg.length - 1;
    >
    > //Time delay between Slides in milliseconds
    > var delay = 3000;
    >
    > var lock = false;
    > var run;
    > function chgImg(directio n) {
    > if (document.image s) {
    > ImgNum = ImgNum + direction;
    > if (ImgNum > ImgLength) {
    > ImgNum = 0;
    > }
    > if (ImgNum < 0) {
    > ImgNum = ImgLength;
    > }[/color]

    Hardly legible.
    [color=blue]
    > document.slides how.src = NewImg[ImgNum];[/color]

    This is proprietary referencing. As "slideshow" is the name
    of an "img" element, use the document.images collection as
    of DOM Level 2 and below:

    document.images['slideshow'].src = ...;
    [color=blue]
    > }
    > }
    > function auto() {
    > if (lock == true) {[/color]

    If `lock' is of type "boolean", no further comparison is required:

    if (lock)

    But you want to avoid globals.
    [color=blue]
    > lock = false;
    > window.clearInt erval(run);
    > }
    > else if (lock == false) {[/color]

    if (!lock)

    But since you invert the value of lock every time, it
    would be much easier to write

    lock = !lock;
    if (!lock)
    {
    // if lock *was* true
    }
    else
    {
    // if lock *was* false
    }

    and since `lock' only reflects `run' converted to boolean,
    `run' can serve as condition here, making the following
    clearInterval() call more reliable and `lock' unnecessary.
    [color=blue]
    > // End -->[/color]

    Nonsense.
    [color=blue]
    > </script>
    >
    >
    > in body....
    >
    > <img src="pic1.jpeg" name="slideshow ">
    > <table>
    > <tr>
    > <td align="right">< a href="javascrip t:chgImg(-1)">Previous</a></td>[/color]

    "javascript :" is nonsense here, see the FAQ. And you do not want to
    use links that only work with JavaScript. Even if you do, write them
    dynamically so that users without scripting do not encounter those
    "malfunctioning " links.
    [color=blue]
    > <td align="center"> <a href="javascrip t:auto()">Auto/Stop</a></td>
    > <td align="left"><a href="javascrip t:chgImg(1)">Ne xt</a></td>
    > </tr>
    > </table>[/color]

    And do not use tables solely for layout purposes.

    This said, a better (yet suboptimal as e.g. not working
    without client-side scripting) way for doing this is this
    untested quick-hack:

    <head>
    ...
    <script type="text/javascript">
    var aImg = new Array(
    {url: "http://pic1.jpeg",
    descr: "pic1"},
    {url: "http://pic2.jpeg",
    descr: "pic2"},
    {url: "http://pic3.jpeg",
    descr: "pic3"},
    {url: "http://pic4.jpeg",
    descr: "pic4"});
    var num = 0;

    function chgImg(directio n)
    {
    var imgs;
    if ((imgs = document.images ))
    {
    num += direction;
    var maxIdx;
    if (num > (maxIdx = aImg.length))
    {
    num = 0;
    }
    else if (num < 0)
    {
    num = maxIdx;
    }

    var curImg = aImg[num];
    imgs['slideshow'].src = curImg.url;

    switch (domLevel)
    {
    case 3: // newer Mozilla/5.0, among others
    descr.textConte nt = curImg.descr;
    break;

    case 2: // older Mozilla/5.0, among others
    descr.firstChil d.nodeValue = curImg.descr;
    break;

    case 0: // IE4, among others
    descr.innerHTML = curImg.descr;
    break;

    case -1: // NN4
    d.open("text/html");
    d.write(curImg. descr);
    d.close();
    }
    }

    return false;
    }

    var run;

    function auto(delay)
    {
    if (run)
    {
    window.clearInt erval(run);
    }
    else
    {
    // setInterval() will not work everywhere;
    // consider to setTimeout() a method
    // that setTimeout()s itself instead
    run = setInterval("ch gImg(1)", delay);
    }

    return false;
    }
    </script>
    ...
    </head>

    <body>
    <div><img src="pic1.jpeg" alt="..." name="slideshow "><br>
    <span id="descr" style="position :relative; top:0"></span></div>

    <script type="text/javascript">
    // DOM feature detection
    var descr;
    if (document.getEl ementById) // DOM Levels 2 & 3
    {
    descr = document.getEle mentById("descr ");
    }
    else if (document.all) // IE4 DOM
    {
    descr = document.all("d escr");
    }
    else if (document.layer s) // NN4 DOM
    {
    descr = document.layers[0];
    }

    var domLevel = -2, d;
    if (descr)
    {
    if (typeof descr.textConte nt != "undefined" )
    {
    domLevel = 3;
    }
    else if (typeof descr.firstChil d
    && typeof descr.firstChil d.nodeValue != "undefined" )
    {
    domLevel = 2;
    }
    else if (typeof descr.innerHTML != "undefined" )
    {
    domLevel = 0;
    }
    // omit this if you do not want to support NN4 anymore
    else if ((d = descr.document)
    && d.open
    && d.write
    && d.close)
    {
    domLevel = -1;
    }
    }

    document.write(
    '<div style="text-align:center">'
    + '<a href="#" onclick="return chgImg(-1);">Previous< \/a>'
    + ' <a href="#" onclick="return auto(3000);">Au to/Stop<\/a>'
    + ' <a href="#" onclick="return chgImg(1);">Nex t<\/a><\/div>');
    </script>


    HTH

    PointedEars

    P.S.
    Your From address is invalid which violates Netiquette and
    Internet/Usenet standards. Change it to avoid to be killfiled.

    Comment

    Working...