Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Is there any way to use a variable for SRC=

    I would like to have the page load a different images depending on SCRIPT variables.
    Something like
    <img src= pictures[n] etc.

    Where pictures is something like
    <SCRIPT language="JavaScript"
    var pictures=[Smilyface.gif,Sadface.gif,mountain.jpg - - - etc];

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    randomly?
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var imgList = ['Smilyface.gif','Sadface.gif','mountain.jpg'];
    
    window.onload = function() {
    thepic=Math.floor(Math.random()*3)
    	document.getElementById('img').src =imgList[thepic];
    }
    </script>
    </head>
    <body>
    <img id="img" src=''>
    </body>
    </html>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript">.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •