3
A [RequireJS](http://requirejs.org)/AMD loader plugin for loading text
6
Known to work in RequireJS, but should work in other AMD loaders that support
7
the same loader plugin API.
11
See the [RequireJS API text section](http://requirejs.org/docs/api.html#text).
15
The latest release is always available from [the "latest" tag](https://raw.github.com/requirejs/text/latest/text.js).
17
It can also be installed using [volo](https://github.com/volojs/volo):
19
volo add requirejs/text
23
It is nice to build HTML using regular HTML tags, instead of building up DOM
24
structures in script. However, there is no good way to embed HTML in a
25
JavaScript file. The best that can be done is using a string of HTML, but that
26
can be hard to manage, particularly for multi-line HTML.
28
The text.js AMD loader plugin can help with this issue. It will automatically be
29
loaded if the text! prefix is used for a dependency. Download the plugin and put
30
it in the app's [baseUrl](http://requirejs.org/docs/api.html#config-baseUrl)
31
directory (or use the [paths config](http://requirejs.org/docs/api.html#config-paths) to place it in other areas).
33
You can specify a text file resource as a dependency like so:
36
require(["some/module", "text!some/module.html", "text!some/module.css"],
37
function(module, html, css) {
38
//the html variable will be the text
39
//of the some/module.html file
40
//the css variable will be the text
41
//of the some/module.css file.
46
Notice the .html and .css suffixes to specify the extension of the file. The
47
"some/module" part of the path will be resolved according to normal module name
48
resolution: it will use the **baseUrl** and **paths** [configuration
49
options](http://requirejs.org/docs/api.html#config) to map that name to a path.
51
For HTML/XML/SVG files, there is another option. You can pass !strip, which
52
strips XML declarations so that external SVG and XML documents can be added to a
53
document without worry. Also, if the string is an HTML document, only the part
54
inside the body tag is returned. Example:
57
require(["text!some/module.html!strip"],
59
//the html variable will be the text of the
60
//some/module.html file, but only the part
61
//inside the body tag.
66
The text files are loaded via asynchronous XMLHttpRequest (XHR) calls, so you
67
can only fetch files from the same domain as the web page (see **XHR
68
restrictions** below).
70
However, [the RequireJS optimizer](http://requirejs.org/docs/optimization.html)
71
will inline any text! references with the actual text file contents into the
72
modules, so after a build, the modules that have text! dependencies can be used
79
The text plugin works by using XMLHttpRequest (XHR) to fetch the text for the
82
However, XHR calls have some restrictions, due to browser/web security policies:
84
1) Many browsers do not allow file:// access to just any file. You are better
85
off serving the application from a local web server than using local file://
86
URLs. You will likely run into trouble otherwise.
88
2) There are restrictions for using XHR to access files on another web domain.
89
While CORS can help enable the server for cross-domain access, doing so must
90
be done with care (in particular if you also host an API from that domain),
91
and not all browsers support CORS.
93
So if the text plugin determines that the request for the resource is on another
94
domain, it will try to access a ".js" version of the resource by using a
95
script tag. Script tag GET requests are allowed across domains. The .js version
96
of the resource should just be a script with a define() call in it that returns
97
a string for the module value.
99
Example: if the resource is 'text!example.html' and that resolves to a path
100
on another web domain, the text plugin will do a script tag load for
103
The [requirejs optimizer](http://requirejs.org/docs/optimization.html) will
104
generate these '.js' versions of the text resources if you set this in the
107
optimizeAllPluginResources: true
109
In some cases, you may want the text plugin to not try the .js resource, maybe
110
because you have configured CORS on the other server, and you know that only
111
browsers that support CORS will be used. In that case you can use the
112
[module config](http://requirejs.org/docs/api.html#config-moduleconfig)
113
(requires RequireJS 2+) to override some of the basic logic the plugin uses to
114
determine if the .js file should be requested:
120
useXhr: function (url, protocol, hostname, port) {
121
//Override function for determining if XHR should be used.
122
//url: the URL being requested
123
//protocol: protocol of page text.js is running on
124
//hostname: hostname of page text.js is running on
125
//port: port of page text.js is running on
126
//Use protocol, hostname, and port to compare against the url
128
//Return true or false. true means "use xhr", false means
129
//"fetch the .js version of this resource".
138
There may be cases where you might want to provide the XHR object to use
139
in the request, or you may just want to add some custom headers to the
140
XHR object used to make the request. You can use the following hooks:
146
onXhr: function (xhr, url) {
147
//Called after the XHR has been created and after the
148
//xhr.open() call, but before the xhr.send() call.
149
//Useful time to set headers.
150
//xhr: the xhr object
151
//url: the url that is being used with the xhr object.
153
createXhr: function () {
154
//Overrides the creation of the XHR object. Return an XHR
155
//object from this function.
156
//Available in text.js 2.0.1 or later.
158
onXhrComplete: function (xhr, url) {
159
//Called whenever an XHR has completed its work. Useful
160
//if browser-specific xhr cleanup needs to be done.
167
### Forcing the environment implemention
169
The text plugin tries to detect what environment it is available for loading
170
text resources, Node, XMLHttpRequest (XHR) or Rhino, but sometimes the
171
Node or Rhino environment may have loaded a library that introduces an XHR
172
implementation. You can foce the environment implementation to use by passing
173
an "env" module config to the plugin:
179
//Valid values are 'node', 'xhr', or 'rhino'
188
Dual-licensed -- new BSD or MIT.
190
## Where are the tests?
192
They are in the [requirejs](https://github.com/jrburke/requirejs) and
193
[r.js](https://github.com/jrburke/r.js) repos.
197
This plugin was in the [requirejs repo](https://github.com/jrburke/requirejs)
198
up until the requirejs 2.0 release.