Use client-side JavaScript in content block
Recently, I attempted to reference a content block from a cloud page that contained client-side JavaScript, and to my surprise, Salesforce Marketing Cloud removed it entirely. It looks like certain hook that triggers when you save your content block doesn;t like client side scripts and are completely removed like comments are when a executable program is compiled.
It took me some time to notice this behaviour. One day when I did not have to smash other marketing automation problem I did some investigation, and lo and behold, it’s actually a common “feature”. With that in mind, I’ve found a simple workaround on YouTube. Let’s take a look at the content block (snippet of code)
%%=Concat('<','scr','ipt>')=%% //your client side javascript var 1plus1 = ()=>1+1 %%=Concat('</','scr','ipt>')=%%
On cloud page we simply reffer to this block by
%%=treatascontent(ContentBlockByKey('client_side_js'))=%%