How-to
Published
Obfuscate the contents of each <script> block, not the HTML file. An obfuscator parses JavaScript, and a page is markup with JavaScript inside it. You either protect one block by hand, or mark the blocks and let a build tool rewrite them in place.
This page shows both ways and the things that break when inline code is renamed: event-handler attributes, script blocks that share variables, Content Security Policy hashes and server-side template tags.
Why a whole page cannot be pasted in
If you paste a complete HTML document into the online obfuscator, it stops and tells you to submit the code inside the script tags instead. That is deliberate. Guessing where JavaScript starts and ends inside markup is how tools corrupt pages, so the hosted engine accepts JavaScript only.
The markup itself is not worth hiding either. The browser has to build the page from it, and the built page is readable in the Elements panel whatever the source looked like. The part with value is the logic.
Method 1: one script block, by hand
For a single page or a quick test:
- Copy everything between
<script> and </script>, without the tags.
- Paste it into the online obfuscator and protect it.
- Paste the output back between the same tags and reload the page.
A small example. The page has a button that calls a function defined in an inline script:
<button onclick="addToCart(3)">Add</button>
<span id="cart-count"></span>
<script>
var cartItems = [];
function addToCart(productId) {
cartItems.push(productId);
var badge = document.getElementById("cart-count");
badge.textContent = "Items: " + cartItems.length;
}
</script>
The script contents after name replacement and string encoding, straight from our engine:
var _$_8261=["\x63\x61\x72\x74\x2D\x63\x6F\x75\x6E\x74","\x49\x74\x65\x6D\x73\x3A\x20"];var cartItems=[];function addToCart(b){cartItems.push(b);var a=document.getElementById(_$_8261[0x0]);a.textContent= _$_8261[0x1]+ cartItems.length}
The element id and the label are no longer readable, the local names are gone, and the button still works, because addToCart kept its name. That last point is the one that matters most for inline code.
The rule for inline code: names the HTML uses must survive
An attribute such as onclick="addToCart(3)" is JavaScript too, but it lives in the markup and never passes through the obfuscator. It finds the function by its global name. The same script with global renaming switched on comes out like this:
var a=["\x63\x61\x72\x74\x2D\x63\x6F\x75\x6E\x74","\x49\x74\x65\x6D\x73\x3A\x20"];var c=[];function b(e){c.push(e);var d=document.getElementById(a[0x0]);d.textContent= a[0x1]+ c.length}
Stronger, and the button is now broken: nothing called addToCart exists. You have three ways out:
- Leave global renaming off for scripts the page calls into by name. This is the default.
- Reserve the names the markup uses, and rename the rest. See name mangling for exclusion lists.
- Remove the dependency. Attach the handler from the script with
addEventListener, and wrap the block in a function so nothing is global. Then every name can be renamed.
The third is the best result: the markup no longer says which function a button runs.
Method 2: mark the blocks and let the CLI rewrite the file
For a site with many pages, do it in the build. Add a marker attribute to each inline script you want protected:
<script data-javascript-obfuscator>
window.releaseWidget = createWidget();
</script>
Then run the CLI over your build output with HTML parsing on:
npx javascriptobfuscator-com --input dist --output dist-protected --preset free --parse-html
What it does, and does not do:
- Only the contents of marked inline blocks are sent for protection and written back in place. The rest of the document is copied untouched.
- Unmarked scripts are left exactly as they are, which is what you want for JSON data blocks, templates and third-party snippets.
- A marker on an external script (
src="...") or a module script is reported as an error instead of being guessed at. Protect those files as JavaScript files.
- Without
--parse-html, an HTML or template file in the input is refused rather than sent to the JavaScript engine by accident.
- Template files are covered as well as
.html: .php, .aspx, .cshtml, .jsp and similar.
Setup, configuration files and CI use are in the npm CLI guide.
Five things to check before you ship
1. Script blocks that share variables
Two inline blocks on one page share the global scope, and each is protected separately. A variable defined in the first and read in the second must keep its name, exactly like a function called from an attribute. Keep global renaming off for those blocks, or merge them into one.
2. A closing script tag inside a string
The HTML parser ends a script block at the first </script> it sees, even inside a JavaScript string. With string encoding on, which every preset has, our engine writes such a string as hex escapes (\x3C\x2F\x73...), so the output cannot close the tag early. If you switch string encoding off, split the string in your source the usual way: "<" + "/script>".
3. Content Security Policy
If your policy allows inline scripts by hash, the hash belongs to the exact text of the block. Protected output is different text, so compute the hash after protection, not before. A nonce is an attribute on the tag and is not affected. A policy with no inline allowance at all is the stronger setup, and one more reason to move code into files.
4. Server-side template tags inside the script
A block such as var user = "{{ name }}"; is not JavaScript until the server has filled it in. Protecting the template source means the obfuscator sees the placeholder, and string encoding will encode the placeholder itself, so the server no longer finds it. Keep server values out of protected code: put them in a small unmarked configuration block or in data- attributes, and read them from the protected script.
5. Runtime defences in small inline blocks
Options such as self-defending code add a few kilobytes of fixed code to every protected unit. On a 200-byte inline block that is most of the output, repeated for each block. Use them on files, not on scattered snippets. Sizes are in how much bigger does obfuscation make your bundle?
When to stop using inline scripts
Inline code is the hardest place to protect logic well. It cannot be renamed freely, it is protected one fragment at a time, and it works against a strict security policy. If a page has more than a few lines of real logic inline, move it into a .js file and leave only configuration in the page. The file then goes through the normal pipeline described in how to obfuscate JavaScript, with every option available.
Whichever way you choose, load the protected page in a browser and click through it before you publish. Testing obfuscated JavaScript lists what to check.
Frequently asked questions
Can I paste a whole HTML file into a JavaScript obfuscator?
No. A JavaScript obfuscator parses JavaScript, and an HTML document is not JavaScript. Either obfuscate the contents of each script block on its own, or use a tool that finds the script blocks for you, such as the CLI's --parse-html mode.
Can the HTML itself be obfuscated?
Not usefully. The browser has to build the page from the markup, and the built page is visible in the Elements panel whatever the source looked like. What can be protected is the logic: the JavaScript that decides what the page does.
Why did my onclick handler stop working after obfuscation?
The attribute calls a global function by name, and the name was changed. Top-level names are kept by default; they are only renamed when global renaming is switched on. Turn it off for scripts that HTML attributes call into, reserve the names, or attach the handler from the script with addEventListener.
Is it better to move inline JavaScript into a separate file?
Usually yes. A separate file goes through the normal build, can be cached, works with a strict Content Security Policy, and can be protected with every option. Keep inline only the small amount of code that has to be there, such as page-specific configuration.
Related reading