How to simulate window.onload in Code Sandbox widget in Slate application

Hi, I recently downloaded a Javascript library and in the tutorial it says to run a particular bit of code from window.onload.

In my Slate application, I added a Code Sandbox widget. In the widget’s properties, I’m pointing to the Javascript library (using its RID). If I put a console.log statement in the Javascript text area, it prints to the console when the application reloads. But if I wrap a console.log statement in a function assigned to window.onload, that statement does not execute.

I see that Slate supports events and actions and I see the event named slate.ready. That seems promising. But I’m not sure how to create an event that invokes code in the Code Sandbox widget.

Thanks,

David

The Code Sandbox widget runs in an iframe and loads the configured javascript from the editor after the iframe has been initialized, so using window.onload will not be possible.

Because the javascript is run after iframe initialization, you could just call your function at the top-level javascript scope, just like you’re doing when you put the console.log directly at the top of the javascript section. This will ensure that it gets run whenever the Code Sandbox iframe refreshes, or loads new javascript to run.

I’d recommend this over trying to attach to slate.ready as slate.ready is triggered when the Slate application has been initialized, but this may happen before the Code Sandbox iframe has fully initialized, causing race conditions. Also, just a reminder not to use Handlebars directly in the javascript section of the Code Sandbox as any changes to javascript during runtime will cause the Code Sandbox iframe to reload, reinitializing all the javascript.

For future reference regarding triggering Code Sandbox Actions via Slate Events, check out the docs on Code Sandbox Slate Functions and the Code Sandbox Interactions tab