Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > loading a string from an external file

Reply
Thread Tools

loading a string from an external file

 
 
mr_burns
Guest
Posts: n/a
 
      04-04-2004
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
 
Reply With Quote
 
 
 
 
Brian Genisio
Guest
Posts: n/a
 
      04-05-2004
mr_burns wrote:

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
errors when loading my .js external file FBro Javascript 2 02-03-2008 06:09 PM
External file loading question M HTML 1 08-03-2007 06:44 PM
[OT] Is loading the second Java application faster than loading the first? David Segall Java 2 01-02-2007 04:41 PM
loading contents of external file into div via iframe crescent_au@yahoo.com Javascript 0 12-06-2006 05:09 AM
Create references to external scipt files from within an external script file Mellow Crow Javascript 6 11-04-2005 01:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57