20

Possible Duplicate:
How can I obfuscate JavaScript?

Hello!

Is it possible to hide JS code? How? I read somewhere a technique like: take JS to external file, and use js-encrypt.

I know that is impossible to hide from all eyes, but...

Thank you.

8
  • 7
    What's so secret about your JS code? Commented Feb 8, 2010 at 14:54
  • 35
    @ceejayoz what's so secret about any code? Kind of a naive question. There are plenty of good reasons for wanting to hide code. Commented Feb 8, 2010 at 14:56
  • 1
    please don't tell me you're hiding passwords or any other sensitive data in javascript. :D Commented Feb 8, 2010 at 14:57
  • 5
    what if i want to sell JS code? And i have several demo code on my site? (i dont want...just an example..) Commented Feb 8, 2010 at 15:03
  • 11
    @apphacker In my experience, the vast majority of people asking about how to protect CSS/HTML/JS code (or disabling right-click on images) are those not at risk of having their code "stolen". Commented Feb 8, 2010 at 15:38

4 Answers 4

12

It is not possible to hide Javascript from the user, since their browser needs to download it to execute it. The only other option is obfuscation.

Sign up to request clarification or add additional context in comments.

4 Comments

I agree; security through obscurity isn't very good security at all...
Well, IMO is not even security.
@chris-l reminds me of a quote from information security class: "Securtity is not hiding the door. It's putting the door in front of them, telling them how it's locked, and they STILL can't get in."
Use Encode.js : encodejs.devincity.com
8

You can run your private JavaScript server side e.g. using ASP JScript.NET

<% WebService Language="JScript" Class="MyClass" %>

You can put the hidden functionality on the server and call it via AJAX in the non hidden part of the code on the client.

you can also use

<script runat="server">

or if you're not using a Microsoft platform you might consider Mozilla Rhino

5 Comments

Seems like a flippant response but it's the only solution to hiding code.
I guess the more modern answer would be to use node.js + socket.io for this kind of thing.
Or if not using a server-side platform that supports JS just use whatever server-side language is supported. Assuming that the code to be hidden is something that can be done on the server at all, for example you can't move a game's event handlers to server-side.
@PeanutPower any pointers on how to do that, any guides or tutorials?
How is Rhino? Can you use it in JSP?
1

As you can take someone else's code and try to encrypt it, same goes for others who can take your code. The only solution that i think is to obfuscate your javascript code so that it becomes little difficult for others to grasp but there again there decryptors available so no real solution as of what i know has come about so far.

Comments

0

The simple answer is no - since the browser needs to understand the javascript, then the user will be able to understand the javascript...

1 Comment

Use Encode.js : encodejs.devincity.com