Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call an array from a different file 1

Status
Not open for further replies.

TheeDave

Technical User
Dec 15, 2010
2
CA
Hi Guys,

I dont use JS much, I did take it in school years ago but cant remember much of it, what Im trying to do is quite simple, just a random quote gen but I want it to get the quots from a separate file as the quotes will appear on every page so I dont want to have to edit each page every time a new one needs to be added.

I just found a simple quote generator online that I want to modify to call the array from a different file, the gen looks like this:


var Quotation=new Array() // do not change this!

Quotation[0] = "Time is of the essence! Comb your hair.";
Quotation[1] = "Sanity is a golden apple with no shoelaces.";
Quotation[2] = "Repent! The end is coming, $9.95 at Amazon.";
Quotation[3] = "Honesty blurts where deception sneezes.";
Quotation[4] = "Pastry satisfies where art is unavailable.";
Quotation[5] = "Delete not, lest you, too, be deleted.";
Quotation[6] = "O! Youth! What a pain in the backside.";
Quotation[7] = "Wishes are like goldfish with propellors.";
Quotation[8] = "Love the river's \"beauty\", but live on a hill.";
Quotation[9] = "Invention is the mother of too many useless toys.";


var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>

If anyone could help me get that array into another file and call it from that script I would really appreciate it.

Thanks guys!

 
Just stick the array into a file and call it anything ou want. but give it an extension of .js such as myarray.js


Then from your page just use a script tag inside our <head> tags with the src pointing to the file to bring it in.

Code:
<head>
...
<script [red]src=[/red][green]"myarray.js"[/green] type="text/javascript"></script>
...
</head>

Then just place the rest of the code wherever it is you want your quotation to appear, between script tags of course.

By the way the creation of the function there seems awfully pointless.

Just call the document.write directly instead.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi Phil, Thank you very much for the replies,

I think Im doing something wrong, Ive created the .js file with my array and placed the code into the page but not getting anything, this is what ive done.

in the .js file I have
var Quotation=new Array()

Quotation[0] = "...and all the quotes

In the HTML head I have:

<script src="../global/java/dyk.js" type="text/javascript"></script>

then in the body I have

<script language="JavaScript">
var Q = Quotation.length;
var whichQuotation=Math.round(Math.random()*(Q-1));
function showQuotation(){document.write(Quotation[whichQuotation]);}
showQuotation();
</script>

What am I misisng?

Thanks again Phil!
 
You aren't missing anything, it should work. Make sure that the js file is really where its pointing it to.

As a test stick the js file in the same folder as your html file, and just call it directly.

<script src="dyk.js" type="text/javascript"></script>

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top