Click to See Complete Forum and Search --> : JavaScripting problem
Digital Dragon
01-02-2003, 07:13 PM
Hello..I need some assistance with Javascripting.
I amanged to compile code that would allow random sayings to be displayed on my page evertime the page is refreshed.
I used notepad to write the code then copied and pasted it into the HTML code where I wanted it to appear.
Problem is it won't work and I am sure I have it right but I am not sure...here is the code I compiled:
<html>
<head>
<body>
<script language="javascript">
QuoteArray = new Array(
"Life is like the game of chess, when it is all over the kings and pawns all go back in the same box",
"I felt like working but after I sat down and had some coffee the feeling soon dissappeared",
"I like swords",
"If you're gonna get mad every time I do something stupid then I guess I'll just have to stop doing stupid things!",
"MWAA-HAHAHAHAHAHAHAHAHAHAHA......",
"There is only one way out and it is being blocked by cannibalistic lawyers.",
"D'oh!",
"I've fallen and I can't get up.",
"Are you sick of these quotes yet?",
"Please, I wanna be a clone now.",
"..........",
"Todays Tip: Don't eat lead!"):
RandomNo = Math.floor(QuoteArray.length * Math.random());
document.write("<b>" + QuoteArray[RandomNo] + "</b>");
</script>
</body>
</html>
Is this right or did I miss something...any help you could lend me would be greatly appreciated.
:D
AdamBrill
01-02-2003, 07:22 PM
At the end of the line of the last array item, you have a colon. Change that to a semi-colon and it should work like a charm. Simple, but fatal, mistake. :)
ChikoritaPro
01-02-2003, 07:23 PM
I'm not used to this style of Arrays, but I'll give a wild guess: I think it is because of the colon (:) you added at the end of the Array, instead of a Semi-Colon. I'm not sure if Array's have a property of lengths, but give it a try :)!
ChadC
01-02-2003, 07:24 PM
Very Very small problem...
"Todays Tip: Don't eat lead!"):
should be
"Todays Tip: Don't eat lead!");
the difference is the semi-colon.
ChadC
LOL guess i got beat to it by Adam and Chikorita
Digital Dragon
01-02-2003, 07:30 PM
Hey thanks!!
Y'know I got the JavaScript from a huge book by Visual on HTML and XHTML....I noticed a few mistakes myself but since I am unfamiliar with JavaScript..I used the basic code as a template....but now I see it was wrong..thanks.:D :D :D :D
Digital Dragon
01-02-2003, 07:34 PM
MWAA-HAHAHAHAHA..
It worked....the solution was so simple yet I didn't see it...thanks again.
Okay, what if I want to display random images? Is the code basically the same only I list the IMG source tags and all...just like in HTML?
ChikoritaPro
01-02-2003, 07:44 PM
In order to display random images, using Arrays, you basically need to know this:
<html>
<head>
<title>Your Title</title>
<script language="JavaScript">
<!--
picturename=new Array()
picturename[0]=new Image(Width, Height)
picturename[0].src="(in quotes, location)"
picturename[1]=new Image(Width, Height)
picturename[1].src="(in quotes, location)"
picturename[2]=new Image(Width, Height)
picturename[2].src="(in quotes, location)"
function randompicture()
{
randomnumber=Math.floor(Math.random()*100%(Modulus by number of pictures, ex. (3+1))+1) // Random number in one line.
document.nameofpicture.src=picturename[randomnumber].src
}
//-->
</script>
</head>
<body onLoad="randompicture()">
<img src=blankimage.gif name=nameofpicture border=0>
</body>
</html>
Basically, 'new Image()' and '.src' allows you to create new Images. You need need to redirect, using Arrays, to the location of the picture (document.nameofpicture.src), and call for the Random picture to start with onLoad="randompicture()". Sorry that I took a while, but that's how to make a random picture rather than a random message. Lol, I bet the others beaten me to it anyways :)!
ChikoritaPro
01-02-2003, 07:46 PM
Just know that in order to replace Images that already exist, then you need a name (name=thename) for an image on the screen and a variable name. You probably can do it alot easier than my complicated method though, lol, using document.write(), but I have not tested that yet. Go ahead and try it if you like :)!
Digital Dragon
01-02-2003, 07:51 PM
Hmmm...I may have to get a book on JavaScripting...I am trying to teach myself but it seems a bit confusing....thanks for the help....I will try it soon.
I copied and pasted the code into Notepad so I can study it....I hope that's okay.
ChikoritaPro
01-02-2003, 08:08 PM
Heh, I don't mind at all that you use the code :)! It may be the first time I made that particular one, but I made that one out of nowhere, lol.
Anyways, basically, there are four new parts:
This style of Array = something[0]="something"
That is the same as doing: something:new Array('something')
Hierarchy (Also known as Document Object Model, but forget the names, heh) : document.nameofpicture.src
document is the entire page you're looking at.
nameofpicture is the name (name=nameofpicture) shown at : <img src=bla.gif name=nameofpicture>
and '.src' is the property of the picture location.
new Images() :
variable=new Image()
variable.src="location.gif"
Please note that this is preloading. It's style is not the same as the hierarchy! '.src' is the picture location of the variable its on. variable.src is its own variable, in fact!
onLoad="functionname()" : This is basically the caller of the function when the page loads.
Yea, it's confusing at start, but you'll get used to it, I know :)!
AdamBrill
01-02-2003, 08:11 PM
Here is an easier way(I think).
<html>
<body>
<img border="0" name=Image>
<script language=javascript>
images = new Array(
"Image1.gif",
"Image2.gif");
RandomNo = Math.floor(images.length * Math.random());
document.Image.src=images[RandomNo];
</script>
</body>
</html>
Just add more into the Array and it should work. BTW, ChikoritaPro, I tried your code, and it didn't work for me. I don't know what was up with that, but...
Digital Dragon
01-02-2003, 08:14 PM
....My brain hurts....
Well, thanks...I only started this a couple of days ago...but I already learned something.
ChikoritaPro
01-02-2003, 08:14 PM
Did you copy and paste it directly? If so, then yep, it wouldn't work, heh, oh well. I'll try it myself and see if I messed up somewhere.
ChikoritaPro
01-02-2003, 08:25 PM
Oops, guess I made a slight error. What you should do is add one to the amount of pictures you have to show in Random when putting it in the Modulus. Otherwise, error :o !