Marketo Landing Pages and custom Javascript code | Community
Skip to main content
New Participant
May 29, 2023
Solved

Marketo Landing Pages and custom Javascript code

  • May 29, 2023
  • 4 replies
  • 5282 views

Hey team, hope you're doing well.

Wanted to know if there is a documentation page that I can use to understand limitations while placing JavaScript code on Marketo Landing Pages.

 

Recently, I have created a class and some portion of the code had the following issue.

 

When I switched the code to be procedural, the code issue was gone and syntax validation was ok.

Thanks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

Actually the validation error shown in your screenshot isn’t surprising at all.  You’re trying to use JS template literal syntax (backticks). That uses the same ${} syntax as Marketo for variable interpolation. So naturally the editor assumes ${this.domain} is a Marketo variable.

 

It doesn’t have to do with object-oriented vs. procedural code but with how you create the string.

 

If you wish to use template literals you can include them in a separate .JS file, which Marketo will not interpolate (like all static assets).

4 replies

New Participant
May 31, 2023

If you want to embed some JS code ( specifically template literals) in your Landing Page then you can apply that in the <head> tag at the Landing Page Level as shown in the screenshot below.

 

SanfordWhiteman
New Participant
May 31, 2023

Fair enough (since the custom <head> won’t be interpolated) but that’s not gonna scale. Best to use an external .JS file for all but the tiniest code snippet anyway, since it’s easier to replace on the fly.

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
May 31, 2023

Actually the validation error shown in your screenshot isn’t surprising at all.  You’re trying to use JS template literal syntax (backticks). That uses the same ${} syntax as Marketo for variable interpolation. So naturally the editor assumes ${this.domain} is a Marketo variable.

 

It doesn’t have to do with object-oriented vs. procedural code but with how you create the string.

 

If you wish to use template literals you can include them in a separate .JS file, which Marketo will not interpolate (like all static assets).

nsoria1Author
New Participant
May 31, 2023

Thank you very much for the response. It looks like I'll need to adapt the code to avoid this kind of syntax issues.

SanfordWhiteman
New Participant
May 29, 2023
You haven't provided enough information here, and troubleshooting a screenshot is not possible. If it's a code question, you should post highlighted code!

One area to bear in mind is variables are not JS-encoded. They are HTML-encoded. So the safe way to use variables in JS is to inject them into an HTML <datalist> and then read them via JS.
Darshil_Shah1
Community Manager
May 29, 2023

Ideally, you should define all the vairables you have used/referenced in your LPs at the top before referecing them in your code. You might have already seen the Marketo defined syntax for the GLTP, but adding here just in case. With that being said, there are some cases, wherein while editing the code in an external code editor, some invisible characters do creep in causing the LP to throw errors while approving (see this thread as an example). You should also consider checking out Greg's post on Best way to add a script in a guided landing page.