+ Reply to Thread
Results 1 to 10 of 10

Thread: Javascript Random qoute

  1. #1
    taha116's Avatar
    taha116 is offline x10 Lieutenant taha116 is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    497

    Javascript Random qoute

    So this is what im basically gonna use for a random tips section in my MMORPG, What i want to know is
    Am i using a method that you wouldn't suggest? Could anyone comeup or suggest a method that wont
    require a link to someone elses site?

    Code:
    <script language="JavaScript">
    // ==============================================
    // Copyright 2004 by CodeLifter.com
    // Free for all; but please leave in this header.
    // ==============================================
    
    var Quotation=new Array() // do not change this!
    
    // Set up the quotations to be shown, below.
    // To add more quotations, continue with the
    // pattern, adding to the array.  Remember
    // to increment the Quotation[x] index!
    
    Quotation[0] = "Time is of the essence! Comb your hair.";
    Quotation[1] = "Sanity is a golden apple with no shoelaces.";
    Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
    Quotation[3] = "Honesty blurts where deception sneezes.";
    Quotation[4] = "Pastry satisfies where art is unavailable.";
    Quotation[5] = "Delete not, lest you, too, be deleted.";
    Quotation[6] = "O! Youth! What a pain in the backside.";
    Quotation[7] = "Wishes are like goldfish with propellors.";
    Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
    Quotation[9] = "Invention is the mother of too many useless toys.";
    
    // ======================================
    // Do not change anything below this line
    // ======================================
    var Q = Quotation.length;
    var whichQuotation=Math.round(Math.random()*(Q-1));
    function showQuotation(){document.write(Quotation[whichQuotation]);}
    showQuotation();
    </script>


    Need help with starting up your website? No problemo PM if you need help, if you want help with scripts like WordPress, SMF and so on dont be afraid to PM for that too.


  2. #2
    taha116's Avatar
    taha116 is offline x10 Lieutenant taha116 is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    497

    Re: Javascript Random qoute

    Bump* Still hopein for an alternative


    Need help with starting up your website? No problemo PM if you need help, if you want help with scripts like WordPress, SMF and so on dont be afraid to PM for that too.


  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript Random qoute

    The JS solution you posted has 3 disadvantages: higher network utilization, requires the browser to support JS and requires editing the source to add or change quotes.

    You could pick the quote server side. Advantages: works if JS is disabled or not supported by the browser and uses less bandwidth. Disadvantage: requires ever so slightly more processing on the server, but not enough to be noticeable, even with hundreds (10**3? 10**4?) of queries per second.

    Note that caching will help with network utilization.
    Last edited by misson; 07-08-2009 at 02:53 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Javascript Random qoute

    Based on misson suggestions, you may find this php code useful.

    Create a txt file with all your quotes/tips. One line per tip. Something like this:

    tips.txt
    Code:
    This is a "test"
    Turn on your computer before you start typing.
    Go to bed early
    Right-click on your desktop and select New > Folder to create a new folder
    Press Change Icon in the Customize tab and select one that's better looking
    It’s a good practice to give rep+ to those who help you.
    In the same directory, lets test some php and see how it works.

    showtip_test.php
    PHP Code:
    <?php
    //Read entire file into an array
    $text file('tips.txt');

    // Loop through array
    foreach ($text as $line_num => $line) {
        echo 
    "Tip #<b>{$line_num}</b> : " $line "<br />\n";
    }

    //Get the total number of items in the array
    $totaltips count($text);

    echo 
    "<br />\n";
    echo 
    "Total # of tips : <b>{$totaltips}</b>  <br />\n";

    //mt_rand : Generate a random value between 0 and total tips
    $randompick mt_rand(0, ($totaltips)-1);

    echo 
    "<br />\n";
    echo 
    "Random number selected : " $randompick "<br />\n";

    echo 
    "<br />\n";
    echo 
    "Display the tip #" $randompick " : <br />\n";
    echo 
    $text[$randompick]; 

    echo 
    "<br />\n";


    ?>
    Call showtip_test.php refreshing the page several times and see if this is what you want

    If it works for you, here you have a condensed version, just echoing what you need:

    showtip.php
    PHP Code:
    <?php
    $tiptext 
    file('tips.txt');
    echo 
    $tiptext[mt_rand(0, (count($tiptext))-1)]; 
    ?>
    You can easily edit your tips.txt to include as many as you want and showtip.php will echo one of those at random.

    If you have an html/php page where you want to display the tip, here is how you can use it:

    example.php
    PHP Code:
    <?php

    //You can have other php code here

    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <meta http-equiv="content-type" content="text/html; charset=windows-1250">
      <title></title>
      </head>
      <body>
      <p>Let me give you this tip:</p>
      <p><h2><?php include('showtip.php');?></h2></p>
      </body>
    </html>

  5. #5
    taha116's Avatar
    taha116 is offline x10 Lieutenant taha116 is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    497

    Re: Javascript Random qoute

    Thank u gormac and mission. im gona try it out ..

    BTW @gor did u write that code urself?
    Last edited by taha116; 07-08-2009 at 10:14 PM.


    Need help with starting up your website? No problemo PM if you need help, if you want help with scripts like WordPress, SMF and so on dont be afraid to PM for that too.


  6. #6
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Javascript Random qoute

    Yes, except for the ‘// Loop through array’ part that I got from somewhere else, sometime ago, probably php.net but I don’t remember. So if you like it, you can use it with no problem.

  7. #7
    taha116's Avatar
    taha116 is offline x10 Lieutenant taha116 is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    497

    Re: Javascript Random qoute

    Ok, Thank you very very much gomarc

    im backing up my site, as i havent done it in a couple days and every day there are atleast half a dozen major changes so for me a couple days of work is allot and then im using your script
    Last edited by taha116; 07-09-2009 at 10:05 AM. Reason: Automerged Doublepost


    Need help with starting up your website? No problemo PM if you need help, if you want help with scripts like WordPress, SMF and so on dont be afraid to PM for that too.


  8. #8
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Javascript Random qoute

    Thanks for the credits, taha116 ! Good luck with your project.

  9. #9
    taha116's Avatar
    taha116 is offline x10 Lieutenant taha116 is an unknown quantity at this point
    Join Date
    Nov 2007
    Posts
    497

    Re: Javascript Random qoute

    No problem Im editing my files now
    Edit:
    Hey im having some trouble i made the files in the right directories and the test worked with the condensed one skipped the other.

    <?php



    ******* = <<<END
    You are exploring the map, and nothing has happened. Continue exploring using the direction buttons or the Travel To menus.
    <p align=center>

    <!-- Begin: AdBrite, Generated: 2009-07-04 19:37:33 -->
    <script type="text/javascript">
    var AdBrite_Title_Color = '191919';
    var AdBrite_Text_Color = '000000';
    var AdBrite_Background_Color = 'FFFFFF';
    var AdBrite_Border_Color = 'CCCCCC';
    var AdBrite_URL_Color = '7F7F7F';
    try{var AdBrite_Iframe=window.top!=window.self?2:1;var AdBrite_Referrer=document.referrer==''?document.lo cation:document.referrer;AdBrite_Referrer=encodeUR IComponent(AdBrite_Referrer);}catch(e){var AdBrite_Iframe='';var AdBrite_Referrer='';}
    </script>
    <script type="text/javascript">document.write(String.fromCharCode(60, 83,67,82,73,80,84));document.write(' src="http://ads.adbrite.com/mb/text_group.php?sid=1242313&zs=3330305f323530&ifr=' +AdBrite_Iframe+'&ref='+AdBrite_Referrer+'" type="text/javascript">');document.write(String.fromCharCode( 60,47,83,67,82,73,80,84,62));</script>
    <div><a target="_top" href="http://www.adbrite.com/mb/commerce/purchase_form.php?opid=1242313&afsid=1" style="font-weight:bold;font-family:Arial;font-size:13px;">Your Ad Here</a></div>
    <!-- End: AdBrite -->
    </p><p align=center><strong>Random Tips</strong><br>

    <script language="JavaScript">

    var Quotation=new Array()

    Quotation[0] = "Hope youre reading these tips! They might just save your life!";
    Quotation[1] = "Make sure you get to the purple dot on the map eventaully, thats your primary quest.";
    Quotation[2] = "Make sure you dont move on right after you kill a monster, always check if there is something usefull it left for you.";
    Quotation[3] = "Playing at higher dificulites may be harder but its more rewarding as well.";
    Quotation[4] = "Having trouble?, use the support forums.";
    Quotation[5] = "You can play with up to 3 charecters per account you know...";
    Quotation[6] = "Dont travel to far away to quickly, the monsters get stronger.";
    Quotation[7] = "Keep an eye on your health bar, the monsters health may be low but yours could be lower.";
    Quotation[8] = "Visit the official site were you will have acess to the official library wich contains some intresting data on monsters";
    Quotation[9] = "Dont pick up a item if your carying one of the same type you like better or it will overwrite your old one";
    Quotation[10] = "Save your extra money in the bank, if you die you will lose all of the money you are carrying.";
    Quotation[11] = "My spelling sucks, i know... Drop by the support forums and report it as a bug.";
    Quotation[12] = "Somtimes traveling to places that look empty might actually lead you to quests... That is usally rare though, try to record the location and post or shout it for other people who might be interested.";

    var Q = Quotation.length;
    var whichQuotation=Math.round(Math.random()*(Q-1));
    function showQuotation(){document.write(Quotation[whichQuotation]);}
    showQuotation();
    </script>


    </p>

    END;
    ?>
    Can u throw in the right peace of code with the right syntax as it dosent seem to work for me and im probably just doin somthin wrong. Ill use whatever u put in and let u know if it works
    Last edited by taha116; 07-09-2009 at 01:01 PM. Reason: Automerged Doublepost


    Need help with starting up your website? No problemo PM if you need help, if you want help with scripts like WordPress, SMF and so on dont be afraid to PM for that too.


  10. #10
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Javascript Random qoute

    Replace all the javascript you have bolded


    Code:
    <script language="JavaScript">
    
    var Quotation=new Array()
    
    Quotation[0] = "Hope youre reading these tips! They might just save your life!";
    Quotation[1] = "Make sure you get to the purple dot on the map eventaully, thats your primary quest.";
    Quotation[2] = "Make sure you dont move on right after you kill a monster, always check if there is something usefull it left for you.";
    Quotation[3] = "Playing at higher dificulites may be harder but its more rewarding as well.";
    Quotation[4] = "Having trouble?, use the support forums.";
    Quotation[5] = "You can play with up to 3 charecters per account you know...";
    Quotation[6] = "Dont travel to far away to quickly, the monsters get stronger.";
    Quotation[7] = "Keep an eye on your health bar, the monsters health may be low but yours could be lower.";
    Quotation[8] = "Visit the official site were you will have acess to the official library wich contains some intresting data on monsters";
    Quotation[9] = "Dont pick up a item if your carying one of the same type you like better or it will overwrite your old one";
    Quotation[10] = "Save your extra money in the bank, if you die you will lose all of the money you are carrying.";
    Quotation[11] = "My spelling sucks, i know... Drop by the support forums and report it as a bug.";
    Quotation[12] = "Somtimes traveling to places that look empty might actually lead you to quests... That is usally rare though, try to record the location and post or shout it for other people who might be interested.";
    
    var Q = Quotation.length;
    var whichQuotation=Math.round(Math.random()*(Q-1));
    function showQuotation(){document.write(Quotation[whichQuotation]);}
    showQuotation();
    </script>
    With this 1 line:

    Code:
    <?php include('showtip.php');?>
    Please note that the files tips.txt and showtip.php must be in the same directory as where you want to call the tips.

    To keep things more organized, you could create a sub-directory named “tips” and put the tips.txt and showtip.php files in there. In this case, your call to the tips would be:

    Code:
    <?php include('tips/showtip.php');?>
    Last edited by gomarc; 07-09-2009 at 02:43 PM.

+ Reply to Thread

Similar Threads

  1. Making a site JavaScript dependent - pros/cons?
    By Tarzan in forum Programming Help
    Replies: 8
    Last Post: 07-11-2008, 10:08 AM
  2. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  3. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  4. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM
  5. Some nice-simple JavaScript effects !!
    By careerbridge in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 07-13-2006, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers