Here is what I want to do, in a browser, thanks to Javascript:
var mytext = '';
mytext += loadfile('intro.md');
mytext += loadfile('chap1.md');
mytext += loadfile('chap2.md');
... other files ...
$('#container').html(markdownrender(mytext));
Note: I do know that in-browser JavaSript is not really made to do such things, I have read many linked questions to this, such as How do I load the contents of a text file into a javascript variable?.
I have two issues:
Cross origin problem
As noted in this answer,
jQuery.get('foo.txt', function(data) { alert(data); });
will be blocked as a Cross-Origin request when used with file://... (Firefox, Chrome, etc.). Even when this error doesn't appear, another one appears: no successful loading andnot well-formed
error. This cannot be solved when using local files, the only solution would be to use a server (see this comment).
Is there any update on this , with new versions of jQuery, since this answer was from 2008?This
XMLHttpRequest
solution works (no Cross-Origin error) but is quite long / verbose. Is there a more modern way to do it, now, 8 years later?
Syncronous / asynchronous
- Even if the previously-mentioned
XMLHttpRequest
solution works, I don't see an elegant way to be sure that fileintro.md
will be finished to be loaded beforechap1.md
, and idem aboutchap2.md
. Then there is a risk the files will be loaded in wrong order. I can see dirty hacks that will work, but no real satisfying solution.
Is there nowadays a modern way to load content from a few static files in (browser) JavaScript?
Note: I'm looking for a solution that also works on local files file://...
, without any server. (I do have servers myself, local, distant, etc. but here I want to find a solution that works even for someone who has only a browser).