Brogramo
Guest
Guest

Parsing JavaScript Strings in AMP: Uncaught Error: Attempting to parse invalid HTML content

If you are using Javascript to pass an HTML string inside an amp-script tag (amp-script), chances are you have encountered this error:

Uncaught Error: Attempting to parse invalid HTML content

Forgetting to add a forward slash to an HTML closing tag, will make AMP complain that you are “Attempting to parse invalid HTML content.” As a result, the following error is generated.

[amp-script] Uncaught Error: Attempting to parse invalid HTML content.
at (amp-script%5Bscript=%22myscript%22%5D.js:1)

But when you do include a forward slash, AMP will complain that you have an “Invalid or unexpected token.”

[amp-script] Uncaught SyntaxError: Invalid or unexpected token
at (blob:https://website.com)

This error will occur when you pass a Javascript string with properly closed HTML tags.

The solution is to escape all HTML tags in your Javascript string.

Example:

mVar = "<div class='myclass'>My text</div>";

Unescaped closing tag: </div>

Escaped closing tag: </div>

Of course, these errors appear in different situations, but what I have mentioned is a valid cause for generating these errors.