Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Help with Javascript code!  (Read 12872 times)

0 Members and 1 Guest are viewing this topic.

lordvader781



    Intermediate
  • Thanked: 1
    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    Re: Help with Javascript code!
    « Reply #15 on: August 13, 2008, 08:15:05 PM »
    Well, first of all I noticed a tag problem.  You somehow have two "</body>" tags.  Your other problem is my fault.  The size of the images array should be two, not three.  Your current example only has two sets of images and links, but the array is sized at three.

    lancebvs

      Topic Starter


      Rookie

      Re: Help with Javascript code!
      « Reply #16 on: August 14, 2008, 07:47:42 AM »
      Ok, so how do I fix the tag problem? Please give me the exact code how it should appear in my page. Also, how do I make it appear in the green box?

      lordvader781



        Intermediate
      • Thanked: 1
        • Certifications: List
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Re: Help with Javascript code!
        « Reply #17 on: August 14, 2008, 03:45:26 PM »
        Here is the correct code.  Of course, you'll have to update the links and images to yours to make it work.  Try this code on that test page you used before.  If this works I'll tell you how to add the image to the green box.

        Code: [Select]
        <html>
          <head>
            <script type="text/javascript">
              function random()
              {
                //The 2 below can be changed to what ever you want
                //depending on the number of images/links you have.
                var images = new Array(2);
                images[0] = new Array(2);
                images[0][0] = "image0.jpg";
                images[0][1] = "page0.html";
                images[1] = new Array(2);
                images[1][0] = "image1.jpg";
                images[1][1] = "page1.html";
                images[2] = new Array(2);
                images[2][0] = "image2.jpg";
                images[2][1] = "page2.html";

                var index = Math.floor(Math.random() * images.length);

                document.getElementById("randomImage").src = images[index][0];
                document.getElementById("randomLink").href = images[index][1];
              }
            </script>
          </head>

          <body onload="random()">
            <a id="randomLink" href=""><img id="randomImage" src="" alt="Random Image" /></a>
          </body>
        </html>

        lancebvs

          Topic Starter


          Rookie

          Re: Help with Javascript code!
          « Reply #18 on: August 14, 2008, 04:51:20 PM »
          It does work:

          http://www.stringsdepotplus.com/news_01.html

          So if you can guide me how to make this work on my homepage that will be great. Also, if possbible how to add the alt text for each image seperately.

          lordvader781



            Intermediate
          • Thanked: 1
            • Certifications: List
            • Computer: Specs
            • Experience: Experienced
            • OS: Windows 7
            Re: Help with Javascript code!
            « Reply #19 on: August 14, 2008, 05:45:12 PM »
            Place this code between the head tags.

            Code: [Select]
            <script type="text/javascript">
                  function random()
                  {
                    //The 2 below can be changed to what ever you want
                    //depending on the number of images/links you have.
                    var images = new Array(2);
                    images[0] = new Array(2);
                    images[0][0] = "image0.jpg";
                    images[0][1] = "page0.html";
                    images[1] = new Array(2);
                    images[1][0] = "image1.jpg";
                    images[1][1] = "page1.html";
                    images[2] = new Array(2);
                    images[2][0] = "image2.jpg";
                    images[2][1] = "page2.html";

                    var index = Math.floor(Math.random() * images.length);

                    document.getElementById("randomImage").src = images[index][0];
                    document.getElementById("randomLink").href = images[index][1];
                  }
            </script>

            Add the function call to your body tag.

            Code: [Select]
            <body onload="random()">

            And finally, add this code to wherever your green box is.

            Code: [Select]
            <a id="randomLink" href=""><img id="randomImage" src="" alt="Random Image" /></a>

            lancebvs

              Topic Starter


              Rookie

              Re: Help with Javascript code!
              « Reply #20 on: August 14, 2008, 07:03:21 PM »
              It works!! Here you can see:

              http://www.stringsdepotplus.com/index.php


              A few questions:

              a) How can I add the alt text to each individual image?
              b) If I want to add another image to the alternating group, what code do I need to edit?

              Thanks!!

              lordvader781



                Intermediate
              • Thanked: 1
                • Certifications: List
                • Computer: Specs
                • Experience: Experienced
                • OS: Windows 7
                Re: Help with Javascript code!
                « Reply #21 on: August 14, 2008, 07:31:42 PM »
                Quote
                a) How can I add the alt text to each individual image?

                To add the alt text to each image you can increase the size of the individual arrays inside the main image array to 3.  The increased size will give you a place to place the text.  You will then have to access the alt attribute of the image tag.

                Quote
                b) If I want to add another image to the alternating group, what code do I need to edit?

                If you want to add another image to the array you must increase the size of the image array.

                Code: [Select]
                <script type="text/javascript">
                      function random()
                      {
                        //The 3 below can be changed to what ever you want
                        //depending on the number of images/links you have.
                        var images = new Array(3);

                        //First image
                        images[0] = new Array(3);
                        images[0][0] = "image0.jpg";
                        images[0][1] = "page0.html";
                        images[0][2] = "image0";

                        //Second image
                        images[1] = new Array(3);
                        images[1][0] = "image1.jpg";
                        images[1][1] = "page1.html";
                        images[1][2] = "image1";

                        //Third image
                        images[2] = new Array(3);
                        images[2][0] = "image2.jpg";
                        images[2][1] = "page2.html";
                        images[2][2] = "image2";

                        var index = Math.floor(Math.random() * images.length);

                        document.getElementById("randomImage").src = images[index][0];
                        document.getElementById("randomLink").href = images[index][1];
                        document.getElementById("randomImage").alt = images[index][2];
                      }
                </script>

                lancebvs

                  Topic Starter


                  Rookie

                  Re: Help with Javascript code!
                  « Reply #22 on: August 15, 2008, 06:11:42 AM »
                  It works. Thanks for all the help.  :)

                  lordvader781



                    Intermediate
                  • Thanked: 1
                    • Certifications: List
                    • Computer: Specs
                    • Experience: Experienced
                    • OS: Windows 7
                    Re: Help with Javascript code!
                    « Reply #23 on: August 15, 2008, 07:22:03 AM »
                    No problem.  Thats why were here.  If you need more help with this or with other Javascript related issues I will be glad to help.  I am also pretty good at CSS so if you need help with that I can assist you as well.