I’m creating a section that has JS inside the section file (inside ).
When I just add the section via the Theme Editor, no JS runs until I save. It doesn’t even run if I edit the section with various fields. It has to be saved in order for the JS to be executed.
I’m using the “shopify:section:load” event listener too, but that is only in use after I save.
Could this be a bug within Shopify???
This is quite problematic because if a customer wants to add a section to see how it will look, but doesn’t want to save because it would affect the live site, he can’t!
Thanks for the reply! Unfortunately, this doesn’t work for me. I thought the raw tags were just for outputting liquid brackets. I can’t even output a console log.
Hey I’m still trying to find a solution for this. Looks like the one you provided is not working. Could you please check what you used? this would help me a lot!!!
thank you for your response. Again to make sure we are talking abou the same problem, the script is not run when you add a section for the first time only. after saving it runs it correctly.
I haven’t found a way to go around that. It looks like Shopify takes the JS added in this way and appends it to a scripts file that it loads. So it only does that once the section is actually saved the first time.
The only way around this is to add the JS to an already existing file that is being loaded separately.
No, I also dont get it why shopify doesnt do a quick fix here… if you have for example a section with blocks and you add a block then the JS suddenly runs, even without saving. I thought they are preventing some XSS attacks but thats not the case if the script runs after deleting or adding a block somewhere on the page without saving the page.
I think what you’re describing is happening because the JS is already loaded on page load. From my experience, if you have inline scripts that have never been saved via the Theme Editor they won’t run until saving.
In your example, the JS could have been saved on a different page with that same section/block and so it was already in the compiled file that Shopify creates.