Is there any way to load some text from another file into javascript, without server side code?
I was thinking to use another element to hold the text inside some comments, but I don't know how to read it's source code with javascript.
Something like:
<script src="myfile.js"></script>
<script> function readMyText() { ... }</script>
In myfile.js:
/* some text */
Building on Pointy's answer, to import from local files do this:
You could also use this to target an external file:
You can put anything you want into a script tag if you give it a "type" that's not something the browser understands as meaning "JavaScript":
You can get the contents via the "innerHTML" property:
Here is a jsfiddle to demonstrate.
That trick is popular lately with people doing client-side page building via templates.
Without using ajax or any server code... sorry mate but you can't :(