Results 1 to 10 of 10

Thread: How can I add document.write to this script??

  1. #1
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default How can I add document.write to this script??

    Hi.
    I have this script here:
    Code:
    <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
    Code:
    document.write
    thing to each quote so I can change their font, size, colors, etc.
    Anyone know how to do that? Thanks.
    Last edited by FrickenTrevor; 08-25-2009 at 08:43 PM. Reason: Typo

  2. #2
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    hmmmmm... this is all between some body tags right? possibly in a div?
    try this:
    Code:
    <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>

  3. #3
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    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:
    Code:
    <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?

  4. #4
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    just remove the stuff in yellow... you already have it in the body... does that help?

  5. #5
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    Quote Originally Posted by thenajsays View Post
    does that help?
    not really, im lost

  6. #6
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    why are you adding document.write to the variables? i dont understand?

  7. #7
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    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)

  8. #8
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    ok... you dont need to add document.write to each array variable, just the HMTL formatting, like so:
    Code:
    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!

  9. #9
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    ok thanks
    and one more thing, how can I change the colors of the text?
    Code:
    r_text[1] = "<span color='red'>And the sky is grey</span>";
    isnt working

  10. #10
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    Code:
    r_text[1] = "<span style='color: red'>And the sky is grey</span>";

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
  •