简体   繁体   中英

Loading strings from external source in Javascript list

I'm looking for the proper way to load strings into Javascript code.

For example if I have about 300 strings like this one:

 '<h1>Some text</h1>' 

I want to load from some external source with loading of page.php into JS script.

I want avoid listing of strings as variables var v1 = '<h1>Some text</h1>'; or in list var lst = ["<h1>Some text</h1>", "<h1>Some text2</h1>"]; included to .js , but load strings to array from outside.

One way is using of MySQL database given the fact that static sequence is important to use ID for assigning string values to special variables.

But I'm not sure if connection with database is a best, fast and soft way for this particular case, when list is not too big, requires static sequence, not dynamically updated and must be outside of .js document and loaded in to array with page load.

I don't see the point of loading from one .js to another .js , if my goal is remove listing from .js . Мaybe I can use .xml , or something else, more suitable what is possible.

You could do so by putting those strings into a Json file

Your Json file (myData.json):

{
 "textOne": "Some text",
 "textTwo": "Some text",
 "textThree": "Some text",
}

Your Javascript:

const messages= require('./myData.json');


const messageOne = messages.textOne;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM