loading a string from an external file

Discussion in 'Javascript' started by mr_burns, Apr 4, 2004.

  1. mr_burns

    mr_burns Guest

    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
     
    mr_burns, Apr 4, 2004
    #1
    1. Advertising

  2. 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
     
    Brian Genisio, Apr 5, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Allen
    Replies:
    8
    Views:
    10,948
    Scott Allen
    May 2, 2004
  2. M
    Replies:
    1
    Views:
    367
    Ed Mullen
    Aug 3, 2007
  3. Mellow Crow
    Replies:
    6
    Views:
    420
    Richard Cornford
    Nov 4, 2005
  4. Replies:
    0
    Views:
    100
  5. FBro
    Replies:
    2
    Views:
    144
    Thomas 'PointedEars' Lahn
    Feb 3, 2008
Loading...

Share This Page