loadtext()
Loads text from the specified text file, for example a JavaScript file (.js) or style sheet (.css).
The text file may be located inside the template or hosted on a Content Management System or on another location outside the template.
Tip: To load an HTML fragment, you can use use loadhtml()
(see loadhtml()); for JSON, use loadjson()
(loadjson()).
Tip: External content is not loaded while editing a script. To test a script that loads external content, you can do a preflight; see Doing a Preflight.
loadtext(location)
Returns the content of a text file. The file extension doesn't have to be .txt. It may also be a JavaScript file (.js) or a style sheet (.css), for instance.
location
String containing a path that can either be a URL or a path that is relative to the section/context.
In order to retrieve files from outside the template the file
protocol is supported as well as the http/https
protocols.
The complete syntax of a fully qualified URL with the "file" protocol is: file://<host>/<path>. If the host is "localhost", it can be omitted, resulting in file:///<path>, for example: file:///c:/somefolder/somecontent.js.
Examples
This script loads a JavaScript file (from the Resources pane) directly into a <script> element.
var js = loadtext("js/my-script.js");
results.append(query("<script>").text(js));
The following script loads a style sheet (from the Resources pane) into the <style> element.
var css = loadtext("css/my-styles.css");
results.append(query("<style type='text/css'>").text(css));