zane
zane

Reputation: 23

Possible to make a javascript "loadable" but not viewable/readable?

Is it possible to have clients have access to load a javascript but still be denied access from accessing the page that it is stored on?

Upvotes: 2

Views: 1261

Answers (4)

user1455310
user1455310

Reputation:

As Nick has recommended, JS file is client-side and you can't restrict it to be viewed from the end users. developers can minimize it and all data which are very important can be moved to server-side like servlets and implementation classes. also, it is a best practice not to access any cookie from the javaScript as a security concern.

Upvotes: 1

Christian Phillips
Christian Phillips

Reputation: 18769

You cant block them from seeing the JavaScript, but you can minify or obfuscate the javascript file, which will make it very difficult to read. Take a look at the links below:

How can I obfuscate (protect) JavaScript?

http://www.jsobfuscate.com/

http://www.minifyjavascript.com/

Upvotes: 3

Ted A.
Ted A.

Reputation: 2302

No. It is not possible to have clients run a JavaScript file without them having access to read the code.

JavaScript is a client-side coding language. That means that the entirety of the file must be served to the client's browser where it is executed.

Once the file is served to the client, they have access to the code and can read everything you have served to them.

You can 'obfuscate' and 'minimize' your .js file. This will still deliver all of the code to the client in a manner readable by a computer, but it will be in a format difficult for a human to read.

Upvotes: 1

Nick Tomlin
Nick Tomlin

Reputation: 29231

This is not possible. Because Javascript is client-side, there is no way to prevent a client from viewing your source without blocking them from using it.

Minification and obfuscation can make it more difficult to discover what is going on in your code, but someone can use a tool like jsbeautifier to thwart your efforts.

You can protect yourself with a license (which you should do, both for yourself and others), but you cannot "lock down" JS in the way that you might with server side languages.

Upvotes: 5

Related Questions