Colorizing a dynamically loaded file
-
27-09-2019 - |
Question
I'm using JQuery to load a file into a textarea and CodeMirror to colorize it, but it seems that they don't play along well: I can get the file to load or the textarea (with existing text) to get colorized, using one or the other, but when I first load it and then colorize it, I end up with a blank textarea. Here is the relevant code of my page:
$("#corpo").load("jquery-latest.min.js"); /* random js file */
var editor = CodeMirror.fromTextArea("corpo", {
parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
path: "/codemirror/js/",
stylesheet: "/codemirror/css/jscolors.css"
});
Thanks!
Solution
Probably you have to put the colorization into a callback:
$("#corpo").load("jquery-latest.min.js", function() {
var editor = CodeMirror.fromTextArea("corpo", {
parserfile: ["tokenizejavascript.js", "parsejavascript.js"],
path: "/codemirror/js/",
stylesheet: "/codemirror/css/jscolors.css"
});
});
... otherwise CodeMirror starts working before the content is fetched (an ajax request in jQuery performs asynchronously).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow