[BUG] Scripts do not work when added dynamically added to iframe <head>
I am using the latest version of grapesjs (0.15.8) and running it on Windows 10 x64 on Electron (this is not a issue with electron) and am facing a problem when I try to dynamically attach scripts to the editor iframe, I have included a link to CodePen for more on this issue. In my project I have to dynamically change scripts. Scripts only work when I add them through canvas: {scripts: ['...']} not when I $(editor.Canvas.getDocument().head).append("...");
Please suggest fix or workaround. (love the project btw)
Hi there! see if this can help you out:
const scriptEl = document.createElement('script');
scriptEl.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js';
editor.Canvas.getDocument().head.appendChild(scriptEl);
cheers!
Thank you very much, the fix worked, but why does it not work with jQuery?
why does it not work with jQuery?
when you use canvas: {scripts: ['...']} to inject a script like jQuery, it will be exposed inside editor Canvas not inside your development environment.
cheers!
Ok thanks a lot.
Hi there! see if this can help you out:
const scriptEl = document.createElement('script'); scriptEl.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'; editor.Canvas.getDocument().head.appendChild(scriptEl);cheers!
Hi! I have recently started working on grapesjs and enjoying it a lot. I am using your solution to add css link elements in head and it works but I have almost 8 css files and code is getting lengthy and repetitive. So, I was wondering if there is any function where I can add all link elements at once. Please help.