+  Scratch Projects Forum
|-+  Programmers Corner» Javascript» Random Story Using Javascript
Username:
Password:
Pages: [1]
  Print  
Author Topic: Random Story Using Javascript  (Read 6317 times)
Scratch Projects
Administrator
novice coder
*****

Karma: +0/-0
Posts: 56


View Profile
« on: March 20, 2006, 01:37:00 PM »

John Fawcett teaches us to create 3 different stories, with a headline, picture, and actual story, and randomly display them one at a time on page load.

Read the full article at http://www.scratchprojects.com/2006/03/random_story_using_javascript_p01.php
Logged
weirdbro
n00b
*

Karma: +0/-0
Posts: 1


View Profile
« Reply #1 on: March 21, 2006, 08:20:26 PM »

The if(ran_num == 0|1|2) parts are useless. There is no difference in the document.write for any of them.
Logged
aboot
n00b
*

Karma: +0/-0
Posts: 8


View Profile
« Reply #2 on: March 22, 2006, 02:46:52 AM »

Quote from: "weirdbro"
The if(ran_num == 0|1|2) parts are useless. There is no difference in the document.write for any of them.


Actually, the if statements for the same document.writes are necessary.  Unless there's some way to cover all the if statements into just one(which I'm sure there is, I just don't know about it...hehe), then you need to cover all possible outcomes or else you'll simply get a blank spot where your script should go.

It's also good to have them all separate like I have for the reason of formatting.  In a real life situation, the stories would probably be quite different, and formatting them dynamically would require 3 different document.writes.
Logged

wnage
Katie
n00b
*

Karma: +0/-0
Posts: 1


View Profile
« Reply #3 on: March 25, 2006, 01:19:52 AM »

"weirdbro" is absolutely right. Since the ran_num variable is being used within the document.write function itself, there's no reason to put if blocks around it. The single document.write section would always be executed, just with whichever variable number you come out with. If you're truly concerned about formatting each story differently, you should have separate css styles created and then track those with another array or some other solution - you shouldn't be formatting with HTML.

I copied the code down and tried it out with a single document.write and it works fine - refreshing randomly changes the content. Note that I had to change the double quotes - the ones that came down when I copied and pasted were invalid and I had to make them regular double quotes. Here's what my code ultimately looked like:

<!--
    var title=new Array();
    title[0]="The First Story";
    title[1]="The Second Story";
    title[2]="The Third Story";
    var ran_pic=new Array()
    ran_pic[0]="images/1.jpg"
    ran_pic[1]="images/2.jpg"
    ran_pic[2]="images/3.jpg"
    var story=new Array()
    story[0]="Well, here's the first story. If I wanna use HTML code that's fine, but make sure the quotations are just apostrophes since we already opened

quotes, Javascript will get confused.For Ex: <a href='another.html'target='_blank'>Another Page</a>";
    story[1]="This is the second story."
    story[2]="And this is the third."
    var ran_num=Math.floor(Math.random()*3)
        document.write(''+title[ran_num]+'<br><table border="0" cellpadding="3"cellspacing="0"><tr><td><img src="'+ ran_pic[ran_num]+'"

border="0"></td><tdvalign="top" class="w">'+story[ran_num]+'</td></tr></table>')

-->

Last, but not least - if you're really serious about doing something like this, you should consider feeding it from a database or XML file instead of hard coding arrays into your .js. Granted, that's additional work but is more maintenance friendly and is better form.
Logged
Kevin
Administrator
31337
*****

Karma: +1/-0
Posts: 137


View Profile WWW
« Reply #4 on: March 25, 2006, 01:54:22 AM »

Thanks Katie for noticing the quotes problem. That was my mistake. There were a few in the other articles as well, so if you see any more, please let me know.
Logged

aboot
n00b
*

Karma: +0/-0
Posts: 8


View Profile
« Reply #5 on: March 27, 2006, 11:00:22 PM »

Ya, I completely understand now that weirdbrow was exactly correct.  For some reason I had this strange impression that I was right...lol....But where I use this code, it is necessary for me to write out the if's because all of my document.write 's need to be different.

I agree with the whole database thing, but I'm not experienced with that yet...and maintenance on my current script is pretty annoying!

I'm trying to work on a way to make a link on each story that creates a new window, and with document.write to create a page displaying all stories.  Seeing as how I'm fairly new to the javascript scene, it's taking me a long time...so if any of you know of an easy way to do so...lemme know please!

and...oh ya thanks guys for editing my noob story!
Logged

wnage
Pages: [1]
  Print  
 
Jump to: