PDA

View Full Version : Consolidate .js Includes


JoeP
05-07-2003, 04:25 PM
I have a page that displays a random fact on entry (Step 1) Below. On The same page I have A Button "Quick Trivia" That Gives A Random Fact on Click Utilizing confirm() alert message. (Step 2.) Below.

Could someone please advise me how to consolidate the two .js files to One. The document.write and confirm() statements use the same array of facts. I currently have two very large .js files, and I would like to consolidate to one .js file.

TIA For any suggestions.


1.

This Displays A Random Fact In A Table Cell:

Page Include script:
<script language="JavaScript1.1" src="QA1_Facts.js"></script>

QA1_Facts.js File:
======
var MyFacts=new Array()
MyFacts[0]='The World\'s Oldest Holiday Inn Is Located In Clarksdale, Mississippi. USA.'
MyFacts[1]='The 3 Writers For The TV Show Dallas Were From Brooklyn Ny And Had Never Been To Texas When They Started To Write The Scripts.'
MyFacts[2]='The First Use Of A Sitcom Laugh Track Was Introduced In 1950 On The Hank Mccune Show.'
MyFacts[3]='....
MyFacts[999]='....

var RandomFact=Math.floor(Math.random()*(MyFacts.length))
document.write('<font Face="Arial" Size="1">' + MyFacts[RandomFact] +'</font>')
======



2.

This Function Is Called From A Button onclick="DisplayFact()"

Page Include script:
<script language="JavaScript1.1" src="QA2_Facts.js"></script>

QA2_Facts.js File:
======
function DisplayFact(){

var MyFacts=new Array()
MyFacts[0]='The World\'s Oldest Holiday Inn Is Located In Clarksdale, Mississippi. USA.'
MyFacts[1]='The 3 Writers For The TV Show Dallas Were From Brooklyn Ny And Had Never Been To Texas When They Started To Write The Scripts.'
MyFacts[2]='The First Use Of A Sitcom Laugh Track Was Introduced In 1950 On The Hank Mccune Show.'
MyFacts[3]='....
MyFacts[999]='....

var ry=Math.floor(Math.random()*MyFacts.length)
if (ry==0)
ry=1
RandomFact = MyFacts[ry]
RandomFact = RandomFact + '\n\nWould You Like Another Fact?'
if (confirm(RandomFact)){DisplayFact()};
}
======

cconn
05-07-2003, 06:18 PM
First thought, why not include the one JavaScript file in the other, by using the typical syntax:

<script language="JavaScript1.1" src="QA1_Facts.js"></script>

QA1_Facts.js File:
======
var MyFacts=new Array()
MyFacts[0]='The World's Oldest Holiday Inn Is Located In Clarksdale, Mississippi. USA.'
MyFacts[1]='The 3 Writers For The TV Show Dallas Were From Brooklyn Ny And Had Never Been To Texas When They Started To Write The Scripts.'
MyFacts[2]='The First Use Of A Sitcom Laugh Track Was Introduced In 1950 On The Hank Mccune Show.'
MyFacts[3]='....
MyFacts[999]='....

var RandomFact=Math.floor(Math.random()*(MyFacts.length))
document.write('<font Face="Arial" Size="1">' + MyFacts[RandomFact] +'</font>')

document.write('<script language="JavaScript1.1" src="QA2_Facts.js"></script>');
======


Admittedly, I didn't try this, but go ahead and give it a shot.

liorean
05-07-2003, 07:08 PM
Just put all code in one single file, see to that there are no name conflicts, and run it- If you get any errors, try checking for names and unclosed parens, braces or brackets.

The array you use, as long as it's the same, only needs to be defined once, so you can erase one of them. Other than that, you can always ask here again if you get some problems with it.

JoeP
05-07-2003, 07:47 PM
Thank you for your time in responding. cconn's example works; however, it dose not consolidate the .js files.

Liorena's tip set me straight. I was declaring the array inside the function. I Declared the array then added the function and then the document,write statement. Works exactly the way I want, only using the one humungous array!:thumbsup: :thumbsup:

Thank You both!