Generate or read a snippet

Use the three actions to prepare line-based strings for existing JavaScript code or recover text from literal document.writeln calls. The converter processes the input in browser JavaScript and displays code as plain text.

  1. Paste <p>Hello</p> and choose HTML to JavaScript. The result is one document.writeln call with the markup inside a quoted string. Less-than characters appear as \u003c escapes, which represent the original characters when the string is read.
  2. For multiple lines, HTML to string array creates one string per line. A followed by a line break and B produces ["A","B"] with formatting whitespace between the entries.
  3. For the reverse action, paste document.writeln("A");document.writeln("B"); and choose JavaScript to HTML. The result is A, a line break, B and a final line break. Copy result copies the displayed text; editing the input clears the previous result.

What is converted and what is rejected

Every writeln call contributes a newline

Forward conversion normalizes CRLF and CR line endings to LF and creates a call for each line, including a final empty line. Input A therefore writes A followed by LF. Input A followed by LF generates two calls and writes A followed by two LF characters.

The reverse action reconstructs the text passed to the document stream, not the original source layout. Blank lines between JS statements do not add output. An empty JS input returns empty text; document.writeln("") returns one LF. A string array does not add newlines to its entries; joining it with \n recovers the normalized input.

Only literal writeln calls are read

The parser accepts direct document.writeln calls with exactly one single- or double-quoted string argument. It decodes JavaScript string escapes without evaluating the program. Whitespace and comments between calls are allowed.

Variables, concatenation, template literals, arrays, document.write, computed property access and other expressions are rejected. Remove surrounding script tags before using the reverse action. HTML entities such as &amp; remain literal text.

String escaping is not HTML sanitization

Quotes, backslashes and control characters are escaped for JavaScript. Escaping < also prevents a literal closing script tag in the generated source, but the decoded HTML still contains its original tags and attributes.

The tool neither sanitizes nor validates HTML. If you execute the snippet elsewhere, document.writeln can insert active HTML and may replace a loaded document. This legacy API is not recommended for new DOM code. Conversion here does not run the snippet or send the input in a conversion request.

HTML and JavaScript conversion questions

Can this turn any JavaScript program into HTML?

No. It only reconstructs direct document.writeln calls containing one quoted string. It does not run functions, fetch data or evaluate expressions.

Will the string array retain apostrophes and backslashes?

Yes. O'Reilly and C:\temp retain their characters as string values. Each input line becomes an array entry, including empty lines; original CRLF or CR endings are normalized to LF.

Recent tools: