I'm loading a fragment of a document via AJAX, and I've managed to load 'external' scripts quite well, but I can't execute all the JavaScript contained inline within <script>
tags.
Here's an example of the document fragment/HTML I'm trying to load:
<textarea></textarea>
<script src="tinyMCE.js" class="ajax-script"></script>
<script class="ajax-script">
alert("I'm inline");
tinymce.init({
selector: 'textarea',
});
</script>
Here's the JavaScript code I use to load this document (on XHR status 200
):
// * This response is HTML
response = xhr.responseText;
// * Set the innerHTML of the body to the response HTML
document.body.innerHTML = response;
// * Find all scripts with ajax-script class
responseScripts = document.getElementsByClassName('ajax-script');
// * Loop through all those scripts
for (i = responseScripts.length; i--;) {
// * Create a 'clone' script element
cloneScript = document.createElement('script');
// * If the response script has a src, add it to the clone
if(responseScripts[0].src) {
cloneScript.src = responseScripts[0].src;
}
// * If the response script has 'inline code', add it
if(responseScripts[0].innerHTML) {
cloneScript.innerHTML = responseScripts[0].innerHTML;
}
// * Remove the original script
responseScripts[0].parentNode.removeChild(responseScripts[0]);
// * Append the clone script to the document
document.body.appendChild(cloneScript);
}
So, with this example, only the alert("I'm inline");
part of the inline code is getting executed, but not the rest. No console errors, no nothing, it just seems like the browser is ignoring the tinymce.init()
part.
I don't know if this has to do with TinyMCE
itself? But why would it? I've also tried evaling the code, but to no luck. After the document has loaded, I can just copy the tinymce.init()
and paste it into the console, and the text editor actually shows (because tinymce.init() gets executed).
Is there any reason you can think of that explains why only the alert
function gets called, but not the rest? Do you see anything wrong with this way of loading the script?
Thanks.