www.webdeveloper.com
Results 1 to 4 of 4

Thread: Need some help

  1. #1
    Join Date
    Jun 2011
    Posts
    2

    Question Need some help

    I am working on a project for a class and need to use javascript to randomly show ads on a webpage. This is my script, what am I doing wrong?

    <script type="text/javascript">


    var rNumber = randInt(5) // generate a random integer from 1 to 5
    var rAd = adDescription(rNumber) // Description of the random ad
    var rLink = adLink(rNumber) // URL of the random ad

    document.write('<a href="' + rLink + '">');
    document.write('<img src="ad('+ rNumber +').jpg" alt= "'+ rAd + '"/>');
    document.write("</a>");

    </script>

    ps I am new to java script

  2. #2
    Join Date
    Jun 2011
    Posts
    43
    Hi. I am also new to java script but i think i can help you.

    Code:
    <script type="text/javascript">
    var rNumber = Math.floor(Math.random() * 5) + 1 // generate a random integer from 1 to 5
    var rAd = adDescription(rNumber) // Description of the random ad
    var rLink = adLink(rNumber) // URL of the random ad
    
    //create an element where you want the ad to appear and give it some ID
    var k='<a href="' + rLink + '">';
    k+='<img src="ad('+ rNumber +').jpg" alt= "'+ rAd + '"/>';
    k+="</a>";
    document.getElementById("id").innerHTML=k;
    
    </script>
    Hope this one works!!

  3. #3
    Join Date
    Jun 2011
    Posts
    2

    left something out

    I left out that randInt(N), adDescription(), and adLink are functions part of external scripts



    <script type="text/javascript">


    var rNumber = randInt(5) // generate a random integer from 1 to 5
    var rAd = adDescription(rNumber) // Description of the random ad
    var rLink = adLink(rNumber) // URL of the random ad

    document.write('<a href="' + rLink + '">');
    document.write('<img src="ad('+ rNumber +').jpg" alt= "'+ rAd + '"/>');
    document.write("</a>");

    </script>

  4. #4
    Join Date
    Jun 2011
    Posts
    43
    Code:
    I left out that randInt(N), adDescription(), and adLink are functions part of external scripts
    doesnt matter . instead of math.random use your function. And keep the rest as it is above. Tell me if it woks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles