tishantha
tishantha

Reputation: 497

Prevent Unauthorized access to JavaScript files

Is there any way to prevent unauthorized access to JavaScript files.

<script src="JS/MyScript.js" type="text/javascript">

Users should not be able to view the MyScript.js file.

I would like to know ,is it possible to do that ? If possible please give me an idea. :)

Thank you..

Upvotes: 0

Views: 883

Answers (2)

Jite
Jite

Reputation: 5847

If the JS file is getting loaded on the client its possible to read it. If you got stuff in the JS file that the user is not supposed to have access to, I would recommend another approach to the problem.

If you don't want it to load at all on the client if its not authorized, you could use some serverside script to prevent it from load.

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337560

It's not possible.

The file must be fully accessible for it to work in your pages. The server has no way to determine if the request was from a script tag, by directly typing it into the browser window, etc.

You can minify your file to make it less human-readable. You can also obfuscate it, although that will have a performance impact on your website. However these are reversible, and anyone who really wants to reverse engineer your code still can.

As with everything though, you should be prepared for anyone to see anything you put online.

Upvotes: 1

Related Questions