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.

Community
  • 1
  • 1

4 Answers4

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.

Dead account
  • 19,587
  • 13
  • 52
  • 82
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

acheo
  • 3,106
  • 2
  • 32
  • 57
  • Seems like a flippant response but it's the only solution to hiding code. – nicerobot Feb 08 '10 at 15:18
  • 2
    I guess the more modern answer would be to use node.js + socket.io for this kind of thing. – acheo Dec 14 '13 at 07:44
  • 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. – nnnnnn Jan 02 '15 at 12:43
  • @PeanutPower any pointers on how to do that, any guides or tutorials? – eozzy Jul 01 '15 at 07:27
  • How is Rhino? Can you use it in JSP? – Micro Sep 09 '15 at 00:23
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.

Rob
  • 26,989
  • 16
  • 82
  • 98
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

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

rATRIJS
  • 309
  • 1
  • 5