Search code examples
javascriptsquarespace

Simple JavaScript formatting


Sorry to be such a noob, but I've looked for this answer and can't find anything relevant.

Im trying to add some code to a squarespace website. The code is to display a random image.

I have to format the code and insert links to where my images are hosted but I'm not sure what to format

This is the bare code:

<script language="JavaScript">
  <!--

  /*
  Random Image Script- By JavaScript Kit (http://www.javascriptkit.com) 
  Over 400+ free JavaScripts here!
  Keep this notice intact please
  */

  function random_imglink() {
    var myimages = new Array()
    //specify random images below. You can have as many as you wish
    myimages[1] = "image1.gif"
    myimages[2] = "image2.gif"
    myimages[3] = "image3.gif"
    myimages[4] = "image4.gif"
    myimages[5] = "image5.gif"
    myimages[6] = "image6.gif"

    var ry = Math.floor(Math.random() * myimages.length)
    if (ry == 0)
      ry = 1
    document.write('<img src="' + myimages[ry] + '" border=0>')
  }
  random_imglink()
  //-->
</script>

<p align="center">This free script provided by<br />
  <a href="http://javascriptkit.com">JavaScript Kit</a>
</p>

And this is the links I've added? have i edited it correctly?

<script language="JavaScript">
  <!--

  /*
  Random Image Script- By JavaScript Kit (http://www.javascriptkit.com) 
  Over 400+ free JavaScripts here!
  Keep this notice intact please
  */

  function random_imglink() {
    var symimages = new Array()
    //specify random images below. You can have as many as you wish
    symimages[1] = "1.jpg"
    symimages[2] = "2.jpg"
    symimages[3] = "3.jpg"
    symimages[4] = "4.jpg"
    symimages[5] = "5.jpg"


    var ry = Math.floor(Math.random() * symimages.length)
    if (ry == 0)
      ry = 1
     document.write('<img src="http://www.mattselley.com/symimages'+symimages[ry]+'" border=0>')
}
    random_imglink()
    //-->
</script>

<p align="center">This free script provided by<br />
  <a href="http://javascriptkit.com">JavaScript Kit</a>
</p>

When I add that code to the squarespace site, in code injection, it doest work, so I'm missing something here, and i think i haven't got the links correct - this is supposed to be a cut and paste code, so I'm doing something wrong.

Any help would be great.

Thanks in advance.


Solution

  • <scripe language="JavaScript">
    var imgs = ['http://lorempizza.com/380/240', 
                'http://dummyimage.com/250/ffffff/000000', 
                'http://lorempixel.com/g/400/200/', 
                'http://lorempixel.com/g/400/200/sports/'];
    var container = document.getElementById('imageContainer');
    var ry = Math.floor(Math.random() * imgs.length)
    if (ry == 0)
      ry = 1
    var img = document.createElement('img');
    img.src = imgs[ry]; // img[i] refers to the current URL.
    container.appendChild(img);
    </script>
    
    
    <div id="imageContainer">  </div>
    

    This is what you want. Just replace my that urls with yours.

    OR if you want to go with your approach..make correction as ..

    document.write('<img src="http://www.mattselley.com/symimages/'+symimages[ry]+'" border=0>')
    

    add / at then end of src.

    Dont forget to Tick and Upvote ;)