I'm trying to set a javascript variable to a html code, but I've noticed something weird. when I write an html tag in a quote (such as body end tag), it formats it as HTML. for example, when I execute the code below in CloudPages:

<!doctype html>
    <meta charset="utf-8">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
      function hi () {
        var code = "</body>";
      window.onload = hi;


My Webpage should contain the "end body tag" text, but instead it looks like this:

enter image description here

like it identifies the "end body tag" as a closing tag, even though it's within a quote. I've also noticed this problem occurs only in CloudPage - When I write this in notepad++ for example and save it as .html, I get the expected result. Does anyone know how to fix this problem?


CloudPages automatically insert a JS snippet at the bottom of the page for tracking purposes (you can see this by viewing the published page's source). To ensure it appears at the bottom (i.e., before the closing </body> tag), it will insert itself by performing a search-and-replace using the string, </body>. For example, if your source code looks like this...

    <p>Hello, world!</p>
    <!-- end of my code -->

It will be altered to look like this after publishing...

    <p>Hello, world!</p>
    <!-- end of my code -->


Because you're using </body> as a string literal in your custom JS, it's being replaced unintentionally (if you view your published page's source, you should notice that the tracking JS snippet has mangled your code variable declaration).

One workaround you could try is to "break-up" your string literal:

var code = "<" + "/body>";

That should leave the custom JS in your example unscathed.

[Edit] The final solution was to use HTML character entity numbers:

var code = "&#60;/body&#62;";
  • Unfortunately, it doesn't work. It outputs nothing, like it's actually formats the "/" or something. Anyway, I'm not sure there is actually a workaround. Thanks anyway. Aug 11 '19 at 5:55
  • @OmriBrandes The fact that you see "nothing" means it's actually working now since "raw" code no longer appears (as it does in your screenshot). You see "nothing" because you don't have any text to display (raw tags won't appear). Try this instead: var code = "Hi!<" + "/body>";
    – Mark G
    Aug 11 '19 at 6:52
  • I know what you meant, but my goal is to actually output "</body>" and not nothing, and in that case I'm not sure there is a solution. Aug 11 '19 at 7:12
  • @OmriBrandes You mean literally? In that case, you'd just need to use the HTML character entity numbers for the "<" and ">" characters: var code = "&#60;/body&#62;";
    – Mark G
    Aug 11 '19 at 7:20
  • Worked. Thank you! Aug 11 '19 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.