...

View Full Version : Reading from .js file??



U-235
04-19-2005, 12:31 PM
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.

martin_narg
04-19-2005, 01:37 PM
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

glenngv
04-19-2005, 01:46 PM
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);

martin_narg
04-19-2005, 01:48 PM
Oops, quite right mate, missed that out as I loaded this little page up and started laughing too hard:

http://forumserver.twoplustwo.com/showflat.php?Cat=&Number=1556673&page=0&view=collapsed&sb=9&o=14&fpart=all&vc=1

U-235
04-19-2005, 01:54 PM
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.

glenngv
04-19-2005, 01:59 PM
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.

U-235
04-19-2005, 02:13 PM
I am using Firefox, opened the Javascript console but it is blank.

Harry Armadillo
04-19-2005, 08:48 PM
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.

U-235
04-20-2005, 02:56 AM
Hooray, it works, thanks for that.

glenngv
04-20-2005, 03:08 AM
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. :D

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum