loading a string from an external file

M

mr_burns

Hi,
i was wondering if it is possible to load text into a string from an
external text file. the reason is that i have a very large string and
it is making my script very messy.

also, is it possible to have some dynamic parts of the text from the
text file? for example, if i load in a string and there is a part of
it that inserts a value from a variable like the following:

'The number of people is ' + var_people_num;

....so the text from the external file would load but these parts would
be given the value of a variable contained in the script that calls
the external file. its not absolutely crucial that i obtain this but
it would help me seperate large chunks that complicate the script. any
ideas would be great. cheers

burnsy
 
B

Brian Genisio

mr_burns said:
Hi,
i was wondering if it is possible to load text into a string from an
external text file. the reason is that i have a very large string and
it is making my script very messy.

also, is it possible to have some dynamic parts of the text from the
text file? for example, if i load in a string and there is a part of
it that inserts a value from a variable like the following:

'The number of people is ' + var_people_num;

...so the text from the external file would load but these parts would
be given the value of a variable contained in the script that calls
the external file. its not absolutely crucial that i obtain this but
it would help me seperate large chunks that complicate the script. any
ideas would be great. cheers

burnsy

Sure... this is easy.

In the main page, add the following:
<SCRIPT type="text/javascript" src=include.js></SCRIPT>

Then, in the included file, named include.js, you can do the following:

////////////////////////////
var long_string = "This is a Test of a Long String with a %VARIABLE1%.";
/////////////////////////////

Then, later on in the main page, you can do something like this:

<SCRIPT type="text/javascript">

var var1 = "variable that can be replaced";
myData = long_string.replace( /%VARIABLE1%/, var1);
alert(myData);

</SCIPT>

Does this do what you need?

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top