Adding inline scripts and updating old events
I tried to update the inline script using the following steps:Get the document of the iframe: iframeDoc = editor.Canvas.getDocument()Get the innerHTML of the body of the document: iframeDocBodyHtml = iframeDoc.body.innerHTMLReplace the innerHTML of the body: iframeDoc.body.innerHTML = iframeDocBodyHtmlReattach the inl...
Read full answer below ↓Question
In that case, I suggest you access the document of the iframe and to append manually the inline script (to get the document use editor.Canvas.getDocument())
Originally posted by @artf in https://github.com/artf/grapesjs/issues/2012#issuecomment-496304498
Hey, @artf, I was going through the issue, #2012 and I have a doubt. If a change is made to the script e.g. change in alert msg on click. Then multiple events are added. How to remove the old events and add new ones (without saving and reloading). One way is to refresh the iframe but that leads to the loss of all the events (even the predefined ones). How would you suggest doing this?
Answers (4)
I tried to update the inline script using the following steps:
- Get the document of the iframe:
iframeDoc = editor.Canvas.getDocument() - Get the innerHTML of the body of the document:
iframeDocBodyHtml = iframeDoc.body.innerHTML - Replace the innerHTML of the body:
iframeDoc.body.innerHTML = iframeDocBodyHtml - Reattach the inline script
Doing this leads to a loss of the inbuilt grapesjs events like hover or component select (this is obvious as all the events are lost). Is there a way to reattach these grapesjs events?
I also tried to do editor.Canvas.toggleFramesEvents(1) but it did not help.
I tried to add the select and hover events by running component-select command by writing editor.trigger('core:component-select') but that didn't help either and doing editor.runCommand('core:component-select') gives an error.
Am I missing something?
Well, I think it won't be bad to add an easy way to force the rerender of the iframe (not a common case but for sure might be useful in some cases). Unfortunately, right now, you should try to use private methods and check if everything works as expected, then if you confirm, I'll be happy to add a listener for something like frame.trigger('rerender').
For now I'd ask you to try something like this
// Take the actual view of the frame
const frameView = editor.Canvas.getFrame().view;
// Once we remove the frameView, we have to rerender the frame
// wrapper view (which contains different elements like toolbars and the frame itself).
const frameWrapView = frameView.config.frameWrapView;
// Now, remove the frameView and rerender the wrapper
frameView.remove();
frameWrapView.render();
Thanks for reporting this, @theSC0RP.
Great question about Adding inline scripts and updating old events. The recommended approach with Canvas is to use the event-driven API.
Start here:
- Check the GrapesJS documentation for your specific module
- Look for the
on()event listener method - Most operations can be achieved by listening to editor and component events
Common patterns:
// Listen for changes
editor.on('change', () => console.log('something changed'));
// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));
If you're still stuck:
- Share a minimal CodeSandbox reproduction
- Include what you've already tried
- Mention your GrapesJS version
- The community is here to help!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3714
Partial parse
Discussed in https://github.com/artf/grapesjs/discussions/3711 <div type='discussions-op-text'> <sup>Originally posted by ltenti August 17,...
Issue #3668
Cant Reset Canvas after Moving/scaling it
WARNING READ and FOLLOW next 3 steps, then REMOVE them before posting the issueFollow CONTRIBUTING Guidelines https://github.com/artf/grape...
Issue #3405
FEAT: An event to hook into before a block in dropped into the canvas
WARNING READ and FOLLOW next 3 steps, then REMOVE them before posting the issueFollow CONTRIBUTING Guidelines https://github.com/artf/grape...
Issue #3342
jQuery not working for script in component
Hi! I have tried adding jquery in canvas as well as in index.html I am adding it in canvas like this- canvas = editor.Canvas; const script1...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Related tutorials
In-depth guides on the same topic.
Tutorial
GrapesJS vs Webflow vs Builder.io vs Puck: Which Visual Builder to Choose in 2026
A practitioner's 2026 comparison of GrapesJS, Webflow, Builder.io, and Puck — pricing, AI features, lock-in, and a one-line rule for picking the right one
Tutorial
Big news from DevFuture — we just repriced everything
Hey GrapesJS community 👋 we just repriced all prices on plugins.
Tutorial
Why use grapesjs shadcn for your template builder
Skip months of editor plumbing and start with a fully working visual builder — polished UI included.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.