background shape
background shape

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'))=%%

Oh hi there 👋
I have a FREE e-book for you.

Sign up now to get an in-depth analysis of Adobe and Salesforce Marketing Clouds!

We don’t spam! Read our privacy policy for more info.

Share With Others

Leave a Comment

Your email address will not be published. Required fields are marked *

MarTech consultant

Marcel Szimonisz

Marcel Szimonisz

I specialize in solving problems, automating processes, and driving innovation through major marketing automation platforms.

Buy me a coffee
Subscribe

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam! Read our privacy policy for more info.

If the form isn't cooperating, don't worry. Just drop us an email at info(at)martechnotes(dot)com with 'Subscribe' in the subject line, and we'll be happy to add you to our list. As a token of our appreciation, we'll also send you a free e-book. Your subscription means a lot to us, and we want to ensure you don't miss out on our valuable content.