0

I have seen that some web sites have a PHP script to redirect the user to another web page if they try to access JavaScript files directly. How is that done?

1
  • 1
    Just so you know.. This may work in a browser, but it won't stop someone who knows what they are doing. All I would have to do is emulate a browser request from your site to get the javascript files. It's incredibly easy to do. I wouldn't waste my time trying to lock someone out of public files. Also, anyone can go into a utility like FireBug and grab your already loaded javascript with 0 effort, no emulation / forging required. Commented Apr 23, 2011 at 2:16

3 Answers 3

1

Unfortunately, as the previous answers have stated, you can't prevent one from seeing the contents of a JS file; if you could do that, how is the browser supposed to parse it?

The next best thing to do is to "Obfuscate" it using something like http://www.javascriptobfuscator.com/default.aspx

This will convert your human-readable code into a bunch of character codes and obscure function names. The Obfuscator I linked to generates a unique ID and bases its encryption on that ID, making it harder to decrypt.

However, this isn't fool-proof, and someone who really wants to get at your JS, for whatever reason, will do it. Anything you really don't want users to have access too should be done server-side. ;)

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

Comments

0

No, that's not possible.

There are plenty of ways how to get JS files. Nothing helps in protection.

Comments

0

Javascript is meant to be client side. That means it always gets executed on the browser which is local and thus can not be hidden.

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.