4

Is there any way to hide the CSS and JavaScript file from the client-side user and still embed it in the webpage shown to the user?

kushpf
  • 1,050
  • 10
  • 21
  • 2
    The most you can do is minify them. – xbonez Aug 08 '12 at 18:59
  • 4
    "here, take this stereo blasting music, but don't listen to the music!" - no, you can't do this. – Marc B Aug 08 '12 at 18:59
  • possible duplicate of [how do i hide javascript code in a webpage?](http://stackoverflow.com/questions/6869312/how-do-i-hide-javascript-code-in-a-webpage) – epascarello Aug 08 '12 at 19:01
  • [How can I obfuscate JavaScript](http://stackoverflow.com/questions/194397/how-can-i-obfuscate-javascript) – epascarello Aug 08 '12 at 19:01
  • 1
    @epascarello - Ya..you can say..but I wanted to know about CSS also..and I searched the question but didn't get these two links; so asked. – kushpf Aug 08 '12 at 19:07

3 Answers3

12

No. CSS and Javascript must be parsable and readable by the browser, therefore a human can get access to the same data.

It is possible to obscure/compress/minify the javascript, but all that generally does is remove whitespace and rename/shorten variable names. The script is still functional and anyone interesting in how it really works can still figure it out (perhaps by using some tools to reformat it) with a little more time.

The typical reasons for minification is to reduce the download size of your scripts (speeding up site performance), but it also has the side effect of making the code harder to read by a human. One should not count on minification providing any real protection as the code can be reformatted and still understood by anyone determined to do so.

If you need to keep something private, keep the logic on the server and don't put it in the client. Clients can access server-based functionality via ajax if need be.

I've never heard of anyone thinking there was a business reason to protect CSS. It's layout/presentation formatting.

jfriend00
  • 580,699
  • 78
  • 809
  • 825
  • Can you please explain me how to keep the logic on the server side? And are they protected from client in every way..or can be accessed via ajax? I can't get what you exactly said in the last line. – kushpf Aug 08 '12 at 19:03
  • 2
    If you have a secret algorithm that you don't want to disclose how it works, then implement that algorithm in server code only (server code is not available to the public like client code is). The algorithm can then be used either at page render time or a client page can request an operation that uses the algorithm via an ajax call. We'd have to know more about what you're trying to protect yourself from to be able to offer more specifics. – jfriend00 Aug 08 '12 at 19:06
  • I was thinking of some code to be implemented and still keep it secret..that's all! But do server side languages offer flexibilities that is comparable to CSS and javaScript? – kushpf Aug 08 '12 at 19:16
  • @k_programmer - Anything you want can be done in server-side languages as they are at least as powerful as client-side languages and even more so in some ways (less restrictions on what a server-side language can do). You can even use javascript server-side if you want. There's no reason I know of to use CSS server-side since the server isn't displaying anything. Display is done on the browser. – jfriend00 Aug 08 '12 at 19:19
1

You can always minify the JavaScript file to make it harder for someone to reads it or to modify it.

For example : http://www.minifyjavascript.com/

You can also do the same thing with CSS.

http://developer.yahoo.com/yui/compressor/ (it can do both JavaScript and CSS)

There are other sites that offers a way to minify the files, but there is no way to hide it completely from the client-side.

Frederic Hutow
  • 144
  • 1
  • 11
1

Minification and base64 encoding. Here's SO questions about base64 encoding. Be mindful that all you're doing is making the process of looking at your code miserable and no more.

Online encoder/decoder.

Community
  • 1
  • 1
vector
  • 6,979
  • 7
  • 48
  • 74