Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-19-2005, 12:31 PM   PM User | #1
U-235
New to the CF scene

 
Join Date: Apr 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
U-235 is an unknown quantity at this point
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.
U-235 is offline   Reply With Quote
Old 04-19-2005, 01:37 PM   PM User | #2
martin_narg
Regular Coder

 
martin_narg's Avatar
 
Join Date: Jul 2002
Location: Chamonix, France
Posts: 600
Thanks: 1
Thanked 3 Times in 3 Posts
martin_narg is an unknown quantity at this point
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
martin_narg is offline   Reply With Quote
Old 04-19-2005, 01:46 PM   PM User | #3
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-19-2005, 01:48 PM   PM User | #4
martin_narg
Regular Coder

 
martin_narg's Avatar
 
Join Date: Jul 2002
Location: Chamonix, France
Posts: 600
Thanks: 1
Thanked 3 Times in 3 Posts
martin_narg is an unknown quantity at this point
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
martin_narg is offline   Reply With Quote
Old 04-19-2005, 01:54 PM   PM User | #5
U-235
New to the CF scene

 
Join Date: Apr 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
U-235 is an unknown quantity at this point
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.
U-235 is offline   Reply With Quote
Old 04-19-2005, 01:59 PM   PM User | #6
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-19-2005, 02:13 PM   PM User | #7
U-235
New to the CF scene

 
Join Date: Apr 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
U-235 is an unknown quantity at this point
I am using Firefox, opened the Javascript console but it is blank.
U-235 is offline   Reply With Quote
Old 04-19-2005, 08:48 PM   PM User | #8
Harry Armadillo
Regular Coder

 
Join Date: Feb 2005
Posts: 400
Thanks: 0
Thanked 0 Times in 0 Posts
Harry Armadillo is on a distinguished road
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.
Harry Armadillo is offline   Reply With Quote
Old 04-20-2005, 02:56 AM   PM User | #9
U-235
New to the CF scene

 
Join Date: Apr 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
U-235 is an unknown quantity at this point
Hooray, it works, thanks for that.
U-235 is offline   Reply With Quote
Old 04-20-2005, 03:08 AM   PM User | #10
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:44 PM.


Advertisement
Log in to turn off these ads.