Click to See Complete Forum and Search --> : Noob needs Cookie / Java help, please!


Jaben
02-12-2003, 09:02 PM
Anybody with know-how:

I recently created a web page with frames.

One of the frames features a quote.

What I would like to happen is have the quote change every time a user visits the page.... but I'm a noob to web design.

I *think* what I need is a cookie / java combo with a persistant cookie -- Assign a number to each quote and then have a script load them in sequence (and, loop them, if possible) per visit.

Also, I'd like to randomize the first visit if possible, so if some dumb bunny deletes their cookies they won't necessarily start over again at the same place.

..I don't know how do do any of this.

Plus, I was wondering if anyone knew if to get this accomplished I actually have to make numberous pages to load into that frame space or can I just have the text I need loaded from a master text file(s)?

Remember, I'm a noob. I'm not computer dumb, just new to java & cookies & such. Any help would be appreciated... Especially if you have code I could just copy and paste.

Thanks.

- Some guy named Ed

AdamBrill
02-12-2003, 09:38 PM
hmm... Is it important that you keep track of where each person is at? I would just make it random... Then, it won't ever go in any certain order. Would that work? If so, here is some code:

<script language=javascript>
arr = new Array("Quote 1",
"Quote 2",
"Quote 3",
"Quote 4",
"Quote 5",
"Quote 6",
"Quote 7",
"Quote 8",
"Quote 9",
"Quote 10",
"Quote 11");
document.write(arr[Math.floor(Math.random()*arr.length)]);
</script>

Hope that helps...

Jaben
02-12-2003, 09:48 PM
yes... yes... simple randomizing would work. good idea.

I saved the code to my PC. appreciate it.

the next question would be (again, please excuse my ignorance here):

is "quote 1" etc. the name of seperate text files? html files? a section on a master file somewhere? or what? I understand the general concept, just not the specific how.



Thanks again!

- Ed

AdamBrill
02-12-2003, 10:12 PM
Well, javascript can not load out of files(beside external .js files), so you would just put all of your quotes in there. Where I have "quote 1", you would put "This is my first quote", and so on. If you want it to load them out of a file(such as a .txt or .dat file), you would have to use PHP or something...

Jaben
02-14-2003, 01:11 AM
...that worked very well once I understood. Thanks. Completly did what I needed it to and was freakin' easy. You rock.

Many thank yous!

- Ed

Jaben
02-14-2003, 01:38 AM
Sorry, one more....

I loaded your script. Worked great. Got to playing with it --

I noticed any time I try to insert break lines or justification parameters into the quotes the entire script stops working.

I need each quote to have right-side justification and a line break like this:

"blah blah blah"

- by so and so

...is that possible?

Thanks again, a lot.

- Ed

AdamBrill
02-14-2003, 08:18 AM
I'm not exactly sure what you wanted, but try this:

"<center>Quote 1<br>by whoever</center>",

That will put the by whoever on the next line and center the whole quote. If that isn't what you wanted, try to explain again... ;)

LAwebTek
02-14-2003, 08:24 AM
<script language="JavaScript" type="text/javascript">
arr = new Array("<p align='left'>Quote 1<br> - By author name 1</p>",
"<p align='left'>Quote 2<br> - By author name 2</p>",
"<p align='left'>Quote 3<br> - By author name 3</p>",
"<p align='left'>Quote 4<br> - By author name 4</p>",
"<p align='left'>Quote 5<br> - By author name 5</p>",
"<p align='left'>Quote 6<br> - By author name 6</p>",
"<p align='left'>Quote 7<br> - By author name 7</p>",
"<p align='left'>Quote 8<br> - By author name 8</p>",
"<p align='left'>Quote 9<br> - By author name 9</p>",
"<p align='left'>Quote 10<br> - By author name 10</p>",
"<p align='left'>Quote 11<br> - By author name 11</p>");
document.write(arr[Math.floor(Math.random()*arr.length)]);
</script>

oops, guess Adam beat me to it! :)

However it might be good to know that if a quote is too long to fit on one line it should be written like this:

arr = new Array("<p align='left'>This is a very, very, very, "
+ "looooooong quote and it spans several lines so now "
+ "it looks like this.<br> - By author 1</p>",
"<p align='left'>Quote 2 is short<br> - By author 2</p>",
etc....

or you can define each quote as a variable which in turn will prevent you from having to use (+) to connect long text strings over several lines:

var Quote1 = "This is quote #1"
var Quote2 = "This is quote #2"
etc.. then,

arr = new Array("<p align='left'>" + Quote1 + "<br> - By author 1</p>",

Jaben
02-14-2003, 03:07 PM
Ya, so I actually figured it out before you responded. (go me)

I was attempting to use \n to do line breaks... then I tried <BR> and <P> -- but I didn't get that javascript, unlike HTML, is case sensitive on these commands.

Whoh nelly.

However, the last two replys you guys left were still very helpful in some of the other commands you included.

Sweet monkey. The page is working great now. Woot.

- Ed