Announcement

Collapse
No announcement yet.

Javascript Issues

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Javascript Issues

    I come to this place again in need of help. I recently added some javascript to the website in order to add a basic changing image thingy. It all worked fine when i was coding it in notepad. However, when i copied the code through into sellerdeck it has stopped working.

    The script has been placed within the <head> tag and the body part has been added to the fragment.

    Code:
    <html> 
    <head> 
    
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    var dimages=new Array();
    var numImages=3;
    for (i=0; i<numImages; i++)
    {
      dimages[i]=new Image();
      dimages[i].src="image"+(i+1)+".jpg";
    }
    var curImage=-1;
    function swapPicture()
    {
      if (document.images)
      {
        var nextImage=curImage+1;
        if (nextImage>=numImages)
          nextImage=0;
        if (dimages[nextImage] && dimages[nextImage].complete)
        {
          var target=0;
          if (document.images.myImage)
            target=document.images.myImage;
          if (document.all && document.getElementById("myImage"))
            target=document.getElementById("myImage");
      
          // make sure target is valid.  It might not be valid
          //   if the page has not finished loading
          if (target)
          {
            target.src=dimages[nextImage].src;
            curImage=nextImage;
          }
          setTimeout("swapPicture()", 5000);
        }
        else
        {
          setTimeout("swapPicture()", 500);
        }
      }
    }
    setTimeout("swapPicture()", 5000);
    //-->
    </SCRIPT>
    
    </head> 
    <body onload="setupPicChange();"> 
    
    <IMG WIDTH=250 HEIGHT=250 ID="myImage" NAME="myImage"
      SRC="images\image0.jpg"></IMG>
    
    
    
    
    </body> 
    </html>
    As soon as i put it into sellerdeck the javascript stops working. Any help would be much appreciated.

    #2
    Replace all [i] with [ i ] to stop SD interpreting them as Variables.

    Repeat for everything in square brackets - e.g. [nextImage] becomes [ nextImage ].
    Norman - www.drillpine.biz
    Edinburgh, U K / Bitez, Turkey

    Comment


      #3
      for some reason it still isnt working. It seems as thought part of the code is working as the image switches but it doesnt work out what image it should be switching to.

      Itsfrustratin that it doesnt just work like it does in notepad.

      Comment


        #4
        Post an update of your script now that you've added the spaces in hte brackets - maybe we can spot something else.

        Comment


          #5
          Preview in Firefox and in FF hit Control / Shift / J to get the Error Console which will provide much better error messages than your "isnt working".
          Norman - www.drillpine.biz
          Edinburgh, U K / Bitez, Turkey

          Comment


            #6
            This would be much better done by using SD Variables to hold these images.

            Create 3 Site level Variables "DImage1", "DImage2" and "DImage3" of type Filename.

            Amend your code to replace:
            Code:
            for (i=0; i<numImages; i++)
            	{
            	dimages[i]=new Image();
            	dimages[i].src="image"+(i+1)+".jpg";
            	}
            With:
            Code:
            dimages[ 0 ]=new Image(); dimages[ 0 ].src = '<actinic:variable name="DImage1" encoding="perl" selectable="false" />';
            dimages[ 1 ]=new Image(); dimages[ 1 ].src = '<actinic:variable name="DImage2" encoding="perl" selectable="false" />';
            dimages[ 2 ]=new Image(); dimages[ 2 ].src = '<actinic:variable name="DImage3" encoding="perl" selectable="false" />';
            Also amend:
            Code:
            <IMG WIDTH=250 HEIGHT=250 ID="myImage" NAME="myImage" SRC="images\image0.jpg"></IMG>
            To be:
            Code:
            <img width="250" height="250" id="myImage" name="myImage" src="<actinic:variable name="DImage1" encoding="perl" selectable="false" />" />
            Now in Settings / Site Options, set DImage1 .. 3 to the files to use.

            N.B. Untested.
            Norman - www.drillpine.biz
            Edinburgh, U K / Bitez, Turkey

            Comment


              #7
              may sound thick but that means nothing to me. The updates script looks like:
              Code:
              <SCRIPT LANGUAGE="JavaScript">
              <!--
              var dimages=new Array();
              var numImages=3;
              for (ii=0; ii<numImages; ii++)
              {
                dimages[ ii ]=new Image();
                dimages[ ii ].src="image"+(ii+1)+".jpg";
              }
              var curImage=-1;
              function swapPicture()
              {
                if (document.images)
                {
                  var nextImage=curImage+1;
                  if (nextImage>=numImages)
                    nextImage=0;
                  if (dimages[ nextImage ] && dimages[ nextImage ].complete)
                  {
                    var target=0;
                    if (document.images.myImage)
                      target=document.images.myImage;
                    if (document.all && document.getElementById("myImage"))
                      target=document.getElementById("myImage");
                
                    // make sure target is valid.  It might not be valid
                    //   if the page has not finished loading
                    if (target)
                    {
                      target.src=dimages[ nextImage ].src;
                      curImage=nextImage;
                    }
                    setTimeout("swapPicture()", 5000);
                  }
                  else
                  {
                    setTimeout("swapPicture()", 500);
                  }
                }
              }
              setTimeout("swapPicture()", 5000);
              //-->
              </SCRIPT>

              Comment


                #8
                Your problem is that you aren't taking into account the way SD Previews images and uploads them. Preview runs from a PreviewHTML folder and needs path names attached to the images to refer back to the Site folder. Live site keeps everything in acatalog on the server and thus uses simple file names.

                All dealt with automatically if you use Variables (see the Starter Guide for a tutorial) or embed proper file names. Much more problematic if you compute file names with JS.

                If you want to persist with your code then keep all images in your Site folder, remove the images/ from your IMG tag, add the files for upload via Design / Additional Files and accept that it won't work in Preview.
                Norman - www.drillpine.biz
                Edinburgh, U K / Bitez, Turkey

                Comment

                Working...
                X