PDA

View Full Version : How can I add document.write to this script??



FrickenTrevor
08-25-2009, 08:43 PM
Hi.
I have this script here:


<script language="JavaScript">
var r_text = new Array ();
r_text[0] = "All the leaves are brown";
r_text[1] = "And the sky is grey";
r_text[2] = "I've been for a walk";
r_text[3] = "On a winter's day";
r_text[4] = "I'd be safe and warm";
r_text[5] = "If I was in L.A.";
r_text[6] = "California dreaming, On such a winter's day";
var i = Math.floor(7*Math.random())
document.write(r_text[i]);
</script>

But I want to add a
document.write thing to each quote so I can change their font, size, colors, etc.
Anyone know how to do that? Thanks.

thenajsays
08-25-2009, 09:20 PM
hmmmmm... this is all between some body tags right? possibly in a div?
try this:

<html>
<head>
<script language="JavaScript">
var r_text = new Array ();
r_text[0] = "All the leaves are brown";
r_text[1] = "And the sky is grey";
r_text[2] = "I've been for a walk";
r_text[3] = "On a winter's day";
r_text[4] = "I'd be safe and warm";
r_text[5] = "If I was in L.A.";
r_text[6] = "California dreaming, On such a winter's day";
var i = Math.floor(7*Math.random());
</script>
</head>
<body>
<div id="leaves" class="yourClass">
<script>
document.write(r_text[i]);
</script>
</div>
</body>

FrickenTrevor
08-27-2009, 04:51 PM
Thanks, and yes, it does go in the body.
As for the example, it works! :)
However, there is one small problem.
When I test the code, the stuff in yellow shows on the page:


<html>
<head>
<script language="JavaScript">
var r_text = new Array ();
r_text[0] = "document.write('<h2>All the leaves are brown</h2>')";
r_text[1] = "And the sky is grey";
r_text[2] = "I've been for a walk";
r_text[3] = "On a winter's day";
r_text[4] = "I'd be safe and warm";
r_text[5] = "If I was in L.A.";
r_text[6] = "California dreaming, On such a winter's day";
var i = Math.floor(7*Math.random());
</script>
</head>
<body>
<div id="leaves" class="yourClass">
<script>
document.write(r_text[i]);
</script>
</div>
</body>

If I try to put those <-- things anywhere in the code, I get an error.
Any help?

thenajsays
08-27-2009, 05:20 PM
just remove the stuff in yellow... you already have it in the body... does that help?

FrickenTrevor
08-27-2009, 07:51 PM
does that help?
not really, im lost :confused:

thenajsays
08-27-2009, 08:44 PM
why are you adding document.write to the variables? i dont understand?

FrickenTrevor
08-28-2009, 02:24 AM
Im trying to get some script that will:
display 5 or more random quotes
be able to add font changes (h2, em, b, span color)

thenajsays
08-28-2009, 07:18 AM
ok... you dont need to add document.write to each array variable, just the HMTL formatting, like so:

var r_text = new Array ();
r_text[0] = "<h2>All the leaves are brown</h2>";
r_text[1] = "<b>And the sky is grey</b>";
r_text[2] = "<em>I've been for a walk</em>";
r_text[3] = "On a winter's day";
r_text[4] = "I'd be safe and warm";
r_text[5] = "If I was in L.A.";
r_text[6] = "California dreaming, On such a winter's day";
cheers!

FrickenTrevor
08-28-2009, 05:08 PM
ok thanks :)
and one more thing, how can I change the colors of the text?

r_text[1] = "<span color='red'>And the sky is grey</span>";
isnt working

thenajsays
08-31-2009, 12:32 PM
r_text[1] = "<span style='color: red'>And the sky is grey</span>";