Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reading from .js file??

    I want to have a random quote at the bottom of several HTML files that is read from a "Master List". To do this I have done as follows

    I have a file called array.js which contains the following...

    var r_text = new Array ();
    r_text[0] = "Some text";
    r_text[1] = "More text";
    .
    .
    .
    r_text[50] = "Final text";
    var i = Math.round(50*Math.random());
    var quote = (r_text[i]);
    return quote;

    Then in the HTML file I have...

    <html>
    <head>
    <TITLE>Page Title</TITLE>
    </head>

    <body>

    --Stuff in body--

    <script src="http://*actual filepath*/array.js" language="javascript" type="text/javascript">

    document.writeln(quote);

    </script>
    </body>
    </html>

    But this does not print anything on the screen. Can anyone please point me in the direction of where I'm stuffing this up.

    Thanks for your help.

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Code:
    var r_text = new Array ();
    r_text[0] = "Some text";
    r_text[1] = "More text"; 
    .
    .
    .
    r_text[50] = "Final text";
    var i = Math.round(50*Math.random());
    var quote = (r_text[i]);
    //The line below is unnecessary
    //return quote;
    
    Then in the HTML file I have...
    
    <html>
    <head>
    <TITLE>Page Title</TITLE>
    </head>
    
    <body>
    
    --Stuff in body--
    
    <script src="http://*actual filepath*/array.js" language="javascript" type="text/javascript">
    
    document.writeln(quote);
    
    </script>
    </body>
    </html>
    This should then work fine.

    Hope this helps.

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You still need to change the random part from:

    var i = Math.round(50*Math.random());

    to:

    var i = Math.floor(Math.random()*r_text.length);
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Oops, quite right mate, missed that out as I loaded this little page up and started laughing too hard:

    http://forumserver.twoplustwo.com/sh...fpart=all&vc=1
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #5
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nope, even with those changes it just doesn't seem to work

    Am I missing something fundamentally important?

    It displays the regular BODY stuff normally, just the ending bit doesn't display at all.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Check for errors. If you're using IE, double click the exclamation icon at the left part of the status bar (or enable error notification in the settings). If you're using Firefox or NS7, open the Javascript Console.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am using Firefox, opened the Javascript console but it is blank.

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A script tag is allowed a src or statements in its body, not both (as alway, IE ignores the standards). Give your document.write its own script tags, or do your document.write at the end of array.js.

  • #9
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hooray, it works, thanks for that.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Harry Armadillo
    A script tag is allowed a src or statements in its body, not both (as alway, IE ignores the standards). Give your document.write its own script tags, or do your document.write at the end of array.js.
    I didn't notice that.

    Or you can just move the document.write statement in the array.js itself so what you have in the body is only the included script.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •