0

i have a cloud page in marketing cloud.

In cloud page, I am using ampscript and javascript both. Can i pass variables values from my ampscript to javascript directly?

I have already tried this example:

%%[

var @CodeP
set @CodeP = RequestParameter("j")

]%%

<script runat="server">

  Platform.Load("Core","1");
  var CodeP = Variable.GetValue("@CodeP");
  Write("<br>CodeP: " + CodeP);

</script>

The problem is that this approach breaks my javascript because my javascript on cloud page starts with this:

 <script type="text/javascript">

Adding runat="server" breaks the existing functionality of my javascript.

please help?

2
  • You don't have to write "type" part, why don't you use it with just <script runat="server">? Aug 20, 2018 at 7:55
  • can you provide more context? This sounds like an outlying syntax issue and not an issue with the GetValue function. Aug 20, 2018 at 12:45

2 Answers 2

7

I think what you are missing here is that Salesforce Marketing Cloud Server Side JavaScript is not the same as JavaScript that runs on the browser. Adding runat="server" to the script block causes your code to be executed by the SFMC server as Server Side JavaScript, meaning if it includes any of the standard browser JavaScript functions like DOM manipulation, it will not work. In fact, it will not even load on the page and will possibly throw a server error due to the limitations of what SFMC will allow you to run server side.

The Variable.GetValue function is specific to Server Side JavaScript and will not work to pass the value of the variable to browser JavaScript. To do so, the best way I have found is to create a hidden form field or div with the value of the variable like this:

<input id="CodeP" type="hidden" value="%%=v(@CodeP)=%%">

Or this:

<div style="display:none;"><p id="CodeP">%%=v(@CodeP)=%%</p></div>

You can then retrieve that value in you JavaScript like this:

<script>
  // Retrieve value of hidden field
  var codeP = document.getElementById("CodeP").value;
  // Retrieve inner html of the p tag
  var codeP = document.getElementById("CodeP").innerHTML;
</script>
1
  • This is exactly what I did as a workaround. Thank you for sharing the info @ncv
    – kuml
    Aug 27, 2018 at 15:06
0

One way of passing variable values from Ampscript block JS is to pass the value to DOM first, then pass the value from DOM, like user @ncv said. But actually we can directly pass the value to JS, without routing through DOM.

%%[
  var @CodeP
  set @CodeP = RequestParameter("j")
]%%

<script>
  var codeP = "%%=v(@CodeP)=%%";
</script>

I think this is a more proper way of doing it. For example, a for loop would be easy with this direct method, but would be a nightmare with the "HTML Routing" method.

<html>
  <ul id="listP"></ul>
</html>

%%[
 set @subscriberRows = RetrieveSalesforceObjects(
"AccountP",
"Listings",
"Key", "=", @keyValue,
)
   
set @rowCount = rowcount(@subscriberRows) 
if @rowCount > 0 then
    var @i
    for @i = 1 to @rowCount do     
      set @row = row(@subscriberRows,@i)
      set @attrP = field(@row,"AttrP")
]%%

 <script>
  $("#listP").append("<li>%%=v(@attrP)=%%</li>");
 </script>

    %%[next @i]%%
 %%[endif]%%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .