0

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..

2
  • What sort of thing is it you want to hide? Code or data? Commented Sep 4, 2013 at 9:14
  • By "Prevent Unauthorized access" do you mean by unauthenticated users or by any user? Commented Sep 5, 2013 at 9:09

2 Answers 2

1

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.

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.