www.webdeveloper.com
Results 1 to 14 of 14

Thread: random text display automatically

  1. #1
    Join Date
    Jun 2011
    Location
    tamilnadu
    Posts
    10

    Question random text display automatically

    hi frndzz

    i need your help please...

    how to display random text automatically with out refreshing webpage using java script ...?????

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373
    Some hints:
    1. Create array, say textArray, of text lines to display
    2. Use Math.floor(Math.random()*textArray.length) to select array element
    3. Update <div id="showText"></div> with a .innerHTML statement.

    Give it a shot if you want to learn.
    Pay someone if you don't want to learn.

  3. #3
    Join Date
    Jun 2011
    Location
    tamilnadu
    Posts
    10
    thanks for your tips


    <script type="text/javascript">
    var textarray = new textarray();
    var num = 0;
    textarray[num++] = "hi"
    textarray[num++] = "hello"
    textarray[num++] = "how r u"
    textarray[num++] = "where r u"

    var rannum=new rannum[Math.random()*textarray.length)
    document.getElementById('ShowText').innerHTML='+textarray[rannum]+';



    </script>
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div id="ShowText">

    </div>
    </form>
    </body>




    is this the way..???

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Lightbulb Something to consider...

    Quote Originally Posted by learnerawaken View Post
    thanks for your tips

    <script type="text/javascript">
    var textarray = new textarray();
    var num = 0;
    textarray[num++] = "hi"
    textarray[num++] = "hello"
    textarray[num++] = "how r u"
    textarray[num++] = "where r u"

    var rannum=new rannum[Math.random()*textarray.length)
    document.getElementById('ShowText').innerHTML='+textarray[rannum]+';

    </script>
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div id="ShowText">

    </div>
    </form>
    </body>

    is this the way..???
    Well, it's an attempt...

    Note changes below compared to what you were trying to do.

    Code:
    <html>
    <head>
    <title>Random Text</title>
    <script type="text/javascript">
    var textarray = [
     "hi",
     "hello",
     "how r u",
     "where r u"    // No comma after last entry
    ];
    
    function RndText() {
      var rannum= Math.floor(Math.random()*textarray.length);
      document.getElementById('ShowText').innerHTML=textarray[rannum];
    }
    onload = function() { RndText(); }
    
    </script>
    </head>
    <body>
    <div id="ShowText"></div>
    </body>
    
    </html>
    BTW: You should enclose your script between [ code] and [ /code] tags (without the spaces)
    to make it easier for forum members to read and test.

  5. #5
    Join Date
    Jan 2011
    Location
    United Kingdom, London
    Posts
    117
    you can add this line
    Code:
    var inter = setInterval(function() { RndText(); }, 2000);
    after
    Code:
    onload = function() { RndText(); }
    to get textRefeshed each 2seconds
    Javascript, jQuery / Prototype & PHP expert ready to complete your work on Freelancer.co.uk - Hire Me at https://www.freelancer.co.uk/users/1...&action=hireme

  6. #6
    Join Date
    Jun 2011
    Location
    tamilnadu
    Posts
    10

    resolved

    thank you very much friends now i go t it work simply superb code......




    <html>
    <head>
    <title>Random Text</title>
    <script type="text/javascript">
    var textarray = [
    "hi",
    "hello",
    "how r u",
    "where r u" // No comma after last entry
    ];

    function RndText() {
    var rannum= Math.floor(Math.random()*textarray.length);
    document.getElementById('ShowText').innerHTML=textarray[rannum];
    }
    onload = function() { RndText(); }

    var inter = setInterval(function() { RndText(); }, 2000);

    </script>
    </head>
    <body>
    <div id="ShowText"></div>
    </body>

    </html>



    very very useful thank youuuuuuuuuuuuuu

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373
    You're most welcome, I'm sure, from both of us.
    Good Luck!

  8. #8
    Join Date
    Jul 2011
    Posts
    2

    Thumbs up

    That was very informative.

    Can you help me in this code to generate the text array with the user input for time.

    Eg. The user can enter the time in minutes, say 5 min. And the code to respond with array in 5 mins.

    Thank you very much in advance.
    (I'm just learning all through my way)

  9. #9
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Question

    Quote Originally Posted by wesleyindia View Post
    That was very informative.

    Can you help me in this code to generate the text array with the user input for time.

    Eg. The user can enter the time in minutes, say 5 min. And the code to respond with array in 5 mins.

    Thank you very much in advance.
    (I'm just learning all through my way)
    I don't understand your request. What is the purpose of waiting 5 minutes to generate an array. What does the array look like?

    Create at least an HTML template of what you expect to see.
    Quote Originally Posted by JMRKER View Post
    Some hints:
    1. Create array, say textArray, of text lines to display
    2. Use Math.floor(Math.random()*textArray.length) to select array element
    3. Update <div id="showText"></div> with a .innerHTML statement.

    Give it a shot if you want to learn.
    Pay someone if you don't want to learn.

  10. #10
    Join Date
    Jul 2011
    Posts
    2

    Smile

    Quote Originally Posted by JMRKER View Post
    I don't understand your request. What is the purpose of waiting 5 minutes to generate an array. What does the array look like?

    Create at least an HTML template of what you expect to see.
    Im creating an Android app, to update the user with a message or array for the time that user has provided. For example he chooses 5 minutes, for every five minutes, he will be updated with a message from the list of arrays we already have in database. So i just thought that this script might take me to some what closer for what im looking for but im finding it hard to achieve it. Thanks for your help.

  11. #11
    Join Date
    Feb 2014
    Posts
    6
    sir can u help me with this..


    <html>
    <script language="javascript" type="text/javascript">
    function randomString() {

    var chars ="";

    var string_length = 4;

    var randomstring = '';

    for (var i=0; i<string_length; i++)
    {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
    }

    document.randform.randomfield.value = randomstring;
    }
    </script>

    <form name="randform">
    <input type="button" value="Create Random String" onClick="randomString();">&nbsp;
    <input type="text" name="randomfield" value="">

    </form>
    </html>

    if i type any random word in textbox then wen i click the button the word well shuffle.
    sample.jpg
    can u send me the code plzzz...............

  12. #12
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373
    Quote Originally Posted by drakezyxel View Post
    sir can u help me with this..


    <html>
    <script language="javascript" type="text/javascript">
    function randomString() {

    var chars ="";

    var string_length = 4;

    var randomstring = '';

    for (var i=0; i<string_length; i++)
    {
    var rnum = Math.floor(Math.random() * chars.length);
    randomstring += chars.substring(rnum,rnum+1);
    }

    document.randform.randomfield.value = randomstring;
    }
    </script>

    <form name="randform">
    <input type="button" value="Create Random String" onClick="randomString();">*
    <input type="text" name="randomfield" value="">

    </form>
    </html>

    if i type any random word in textbox then wen i click the button the word well shuffle.
    sample.jpg
    can u send me the code plzzz...............
    A couple of things you should take note of:
    1. It is better to start your own thread of questions than to hijack another's.
    I will get more interest than reading through a number of answers that may not apply to your problem.
    2. You should enclose your script between [ code] and [ /code] tags (without the spaces)
    to make the script easier to read, copy , test, etc. And it retains your formatting.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    
    </head>
    <body>
    <form name="randform">
    <input type="button" value="Create Random String" onClick="randomString()">&nbsp;
    <input type="text" id="randomfield" value="test">
    <div id="debug"></div>
    
    <script language="javascript" type="text/javascript">
    
    // randomly order the questions...
    function randOrd() { return (Math.round(Math.random())-0.5); } // quick and dirty randomizer (not the best)
    
    function randomString() {
      var inputStr = document.getElementById('randomfield').value;
      var inputArr = inputStr.split('')
      inputArr.sort(randOrd);
      var outputStr = '';
      for (var i=0; i<inputArr.length; i++) { outputStr += inputArr[i]; }
      document.getElementById('debug').innerHTML += outputStr+'<br>';
    }
    </script>
    
    </form>
    </body>
    </html>

  13. #13
    Join Date
    Jun 2014
    Posts
    2
    how to add button in this i tried not working,,wer to add can u pls help mee
    <html>
    <head>
    <script>
    var request = new XMLHttpRequest();
    request.onload = function() {
    // get the file contents
    var fileContent = this.responseText;
    // split into lines
    var fileContentLines = fileContent.split( '\n' );
    // get a random index (line number)
    var randomLineIndex = Math.floor( Math.random() * fileContentLines.length );
    // extract the value
    var randomLine = fileContentLines[ randomLineIndex ];

    // add the random line in a div
    document.getElementById( 'random-phrase' ).innerHTML = randomLine;
    };
    request.open( 'GET', 'animals.txt', true );
    request.send();
    </script>
    </head>
    <body>
    <div id="random-phrase"></div>
    </body>
    </html>

  14. #14
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373
    Quote Originally Posted by santhosh2803 View Post
    how to add button in this i tried not working,,wer to add can u pls help mee
    <html>
    <head>
    <script>
    var request = new XMLHttpRequest();
    request.onload = function() {
    // get the file contents
    var fileContent = this.responseText;
    // split into lines
    var fileContentLines = fileContent.split( '\n' );
    // get a random index (line number)
    var randomLineIndex = Math.floor( Math.random() * fileContentLines.length );
    // extract the value
    var randomLine = fileContentLines[ randomLineIndex ];

    // add the random line in a div
    document.getElementById( 'random-phrase' ).innerHTML = randomLine;
    };
    request.open( 'GET', 'animals.txt', true );
    request.send();
    </script>
    </head>
    <body>
    <div id="random-phrase"></div>
    </body>
    </html>
    This is a VERY OLD thread you are adding to as it was last opened in Dec of 2005.
    You will get a better response if you start your own thread rather than adding to this one.
    You can reference this thread, but some members won't respond to a thread this old
    as many JS changes may have occured over the years.

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