Results 1 to 10 of 10

Thread: How do I make random sentences appear when page refreshes

  1. #1
    Join Date
    Aug 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I make random sentences appear when page refreshes

    I'm not sure if I need php for this to work, but I've seen it done with seperate JS files in the same directory.

    I've been looking at other sites to see how they do it, but I can never view the changing script! What do I need to put on the page to make the changing script appear on the page?

  2. #2
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Put that into the HEAD section of your page:
    Code:
    <script type="text/javascript">
    var sentences = new Array(
    	"Put your sentences here.",
    	"Quoted and comma delimited.",
    	"Third sentence.",
    	"Fourth sentence.",
    	"And so on...",
    	"<em>HTML tags may be used</em>",
    	"Use <br /> for line breaks.",
    	"NO COMMA AFTER THE LAST ONE!!!"
    );
    </script>
    And that, where you want the sentences to appear:
    Code:
    <script type="text/javascript">
    document.write(sentences[Math.floor(Math.random()*sentences.length)]);
    </script>
    Last edited by DimX; 08-22-2006 at 10:54 AM.

  3. #3
    Join Date
    Aug 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It didn't work! Nothing shows up on the page!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title>Random Sentence w/Cookie script - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <span id="theS"></span>
    <script type="text/javascript">
    /* Random Sentence w/Cookie script
     * All credits must remain for legal use */
     
    // Cookie code from: http://www.quirksmode.org/js/cookies.html
    function createCookie(name,value) {
    	document.cookie = name+"="+value+"; path=/";
    }
    
    function readCookie(name) {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++) {
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
    }
    
    function rs(){  // Random Sentence on Refresh w/Cookie - jscheuer1 - http://www.dynamicdrive.com/forums
    var s=[
    'Hey there',
    'How there',
    'Hey now',
    'Way home',
    'All now'];
    s.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
    var sn=readCookie('sen')&&readCookie('sen')==s[0]? s[1] : s[0];
    document.getElementById('theS').innerHTML=sn;
    createCookie('sen',sn)
    }
    rs();
    </script>
    </body>
    </html>
    Last edited by jscheuer1; 08-22-2006 at 10:42 AM. Reason: simplify
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Quote Originally Posted by thinandpale
    It didn't work! Nothing shows up on the page!
    Whoops, sorry, my bad, there was a typo in the code try it again, it works now.

  6. #6
    Join Date
    Aug 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yeah, that was it......... that worked!

    Thank you very much. I've been searching the whole web for the last 20 hours trying to figure this out. I read pages after pages of .js and .php and other message boards weren't able to help.

    Thanks for helping me.

    I was trying to use the Array method, but this works.

    Is there a way for me to use mp3 embed tags instead of sentences ?

    or do I need to make a seperate page listing the mp3 files and then if so what do I do to get those mp3 songs to play at random when the page refreshes.

  7. #7
    Join Date
    Aug 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I made a sample page for both scripts. They both work.

    Is there a way for me to use mp3 files ?

  8. #8
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Where ever you put the string to display put the HTML for your mp3.

  9. #9
    Join Date
    Aug 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I just figured it out....

    I needed to put \ infront of " in the embed so it could read it

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Thought that I would just throw in here that my version, because it uses cookies, is much less likely to ever randomly repeat. Cookies would have to be disabled for that to happen.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •