loading a string from an external file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mr_burns

    loading a string from an external file

    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
  • Brian Genisio

    #2
    Re: loading a string from an external file

    mr_burns wrote:
    [color=blue]
    > 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[/color]

    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.rep lace( /%VARIABLE1%/, var1);
    alert(myData);

    </SCIPT>

    Does this do what you need?

    Brian

    Comment

    Working...