grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

[BUG] Scripts do not work when added dynamically added to iframe <head>

Open TheComputerM opened this issue 7 years ago • 5 comments

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("...");

CodePen

Please suggest fix or workaround. (love the project btw)

TheComputerM avatar Oct 07 '19 03:10 TheComputerM

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!

pouyamiralayi avatar Oct 07 '19 16:10 pouyamiralayi

Thank you very much, the fix worked, but why does it not work with jQuery?

TheComputerM avatar Oct 08 '19 13:10 TheComputerM

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!

pouyamiralayi avatar Oct 08 '19 18:10 pouyamiralayi

Ok thanks a lot.

TheComputerM avatar Oct 09 '19 14:10 TheComputerM

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.

RutujaBadbe avatar Feb 27 '21 11:02 RutujaBadbe