CompleteSEOTools
Home › Developer Tools › HTML to JS Variable

HTML to JS variable

Paste a block of HTML and get a string variable with the quotes and line breaks already escaped.

About HTML to JS Variable

Putting HTML inside a string by hand means escaping every quote and backslash and breaking the lines in the right places. This tool does that for you. Choose single quotes, double quotes or a template literal for JavaScript, or a plain string or a text block for Java. You can keep the line breaks or squash the markup onto one line. It also protects the closing script tag, which would otherwise end an inline script early.

  • Storing a snippet in a JavaScript variable for innerHTML or a template.
  • Adding HTML to a Java class or a unit test.
  • Avoiding broken quotes in a long block of markup.
  • Embedding a widget script that sits inside another script tag.

How to use HTML to JS Variable

1

Step 1

Paste your HTML and pick the language and quote style.

2

Step 2

Choose whether to keep line breaks and set the variable name.

3

Step 3

Copy the declaration into your code.

Good to know

  • innerHTML runs whatever markup it is given. Only insert HTML you trust, or build the nodes with the DOM instead.
  • A Java text block needs Java 15 or newer. Older versions need the concatenated style.
  • Template literals treat the dollar-brace sequence as code, so the tool escapes it for you.

Common questions

What does escaping the closing script tag mean?

A browser ends an inline script at the first closing script tag it sees, even inside a string. Writing it with a backslash before the slash avoids that and gives the same text at run time.

Does it minify my HTML?

Only if you choose the single line option, which joins the lines and trims leading spaces. It does not rewrite the markup itself.

Which characters get escaped?

Backslashes, the quote character you chose, line breaks and tabs. In template literals it also escapes backticks and the dollar-brace sequence.