How to prevent your JavaScript code from being stolen, copied, and viewed? [closed]

It’s simply not possible.

For a visitor’s browser to be able to execute the script, they have to be able to download it. Not matter what trickery you try to pull with JS, server permissions etc., at the end of the day they can always just wget http://example.com/yourcoolscript.js. And even if they can’t (e.g. you require “secret” headers for that request) that would likely inhibit the behaviour of most browsers, while not stopping a determined person from looking anyway.

Fundamentally, because JS is executed client-side, the client must have access to the “original” JS file.

One minor thing you can do is obfuscation, which can help a little bit. But since JS is interpreted, it’s also its own deobfuscator – see one of my earlier answers for an example.

Basically – “if you build it, they will look”. 🙂

Leave a Comment