SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Script to randomly select from 10 javascripts...

    Hello,

    Quick question that will turn into a long one...

    First, lets say I had 10 different Javascripts to use on a page, in one area. I don't want all 10 displayed at one time, simply 1 random one from the 10.

    Could this be done?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    There are a couple of primary options available.

    1. Have the server pick a random number and from that embed one of the scripts into the code
    2. Have all the scripts available locally, and pick a random number to call one of them.
    Last edited by paul_wilkins; Feb 27, 2008 at 12:02.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
      
    <script type="text/javascript">
    
    function f1() {alert("f1");}
    function f2() {alert("f2");}
    function f3() {alert("f3");}
    function f4() {alert("f4");}
    function f5() {alert("f5");}
    function f6() {alert("f6");}
    function f7() {alert("f7");}
    function f8() {alert("f8");}
    function f9() {alert("f9");}
    function f10() {alert("f10");}
    
    var r=(Math.round(Math.random()*898))&#37;10;
    var arr=[f1, f2, f3, f4, f5, f6, f7, f8, f9, f10]; 
    window.onload=arr[r];
    
    </script>
    Bismillahirrahmanirrahîm
    Bizi doğru yola, kendilerine nimet verdiklerinin yoluna ilet; gazaba uğrayanların ve sapıklarınkine değil.


Bookmarks

Posting Permissions

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