Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    1
    Member #
    20184
    Pretty new to Javascript and I was just wondering if someone could help me out.

    I'm making a web page where a random phrase is displayed using the following javascript:

    [I]<script>
    var r_text = new Array ();
    r_text[0] = "Everyone is kneaded out of the same dough but not baked in the same oven";
    r_text[1] = "Education is learning what you didn't even know you didn't know";
    ...
    r_text[88] = "From the errors of thers, a wise man corrects his own.";
    var i = Math.round(88*Math.random());

    document.write(r_text);</script>

    The script runs when the page is loaded initially but I would like to "rerun" the code so that a new phrase is displayed when the user clicks a button, surely this is possible?

    Extremely grateful for any help you can give me.

  2.  

  3. #2
    Senior Member paintingtheweb's Avatar
    Join Date
    Jul 2007
    Location
    Las Vegas
    Posts
    128
    Member #
    15503
    I would put it into a function and call it when needed. like so:

    function writeRandomQuote(){
    var r_text = new Array ();
    r_text[0] = "Everyone is kneaded out of the same dough but not baked in the same oven";
    r_text[1] = "Education is learning what you didn't even know you didn't know";
    ...
    r_text[88] = "From the errors of thers, a wise man corrects his own.";
    var i = Math.round(88*Math.random());

    document.write(r_text);
    }

    You could call it when the document loads and on the click event of whatever you want. If you're not using any frameworks (like jQuery) you could call it on the onLoad event for the body to load it up the first time and on the onClick event for the buttons. If you were using jQuery, you could just use the document.ready function. Example below:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    function writeRandomQuote(){
    var r_text = new Array ();
    r_text[0] = "Everyone is kneaded out of the same dough but not baked in the same oven";
    r_text[1] = "Education is learning what you didn't even know you didn't know";
    ...
    r_text[88] = "From the errors of thers, a wise man corrects his own.";
    var i = Math.round(88*Math.random());

    document.write(r_text);
    }

    $(document).ready(function(){
    writeRandomQuote();
    $('button').click(function(){
    writeRandomQuote();
    });
    });
    </script>


Remove Ads

Posting Permissions

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

Search tags for this page

how to rerun a code in javascript
,
how to rerun a program in javascript
,

html rerun the javascript

,
javascript how to rerun same function
,
javascript re-run script
,
javascript rerun
,
rerun a javascript jquery
,

rerun javascript

,
rerun js html
,
rerun script html
Click on a term to search for related topics.
All times are GMT -6. The time now is 12:16 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com