5

As programmers, you have certain curtains to hide behind with your code. With PHP all of your code is server side preprocessed, so this never see's the light of day as far as the user is concerned. If you have maybe rushed through some code for a deadline, as long as it functions correctly then the user never needs to know how many expletives you've inserted into the comments. However with more and more applications being written for the web, with a desktop feel implemented by AJAX and popular frameworks like jQuery being banded around to every Tom, Dick and Harry, how can a programmer maintain some dignity and hide his/her JavaScript code without it being flaunted like dirty laundry when the users hit Right Click->View Source or Inspect Element. Are there any ways to hide JavaScript application logic/code?

5
  • 6
    No.............. Jan 31, 2011 at 10:35
  • Nice hack for the character limit. There is no long way to say "No" is there? :) Jan 31, 2011 at 10:37
  • 8
    I love the title of this question :)
    – wildpeaks
    Jan 31, 2011 at 10:56
  • Why do you want to hide it?
    – Marcie
    Jan 31, 2011 at 18:19
  • Was just interested in the options... Jan 31, 2011 at 18:52

6 Answers 6

10

I don't think that hiding your javascript for the reason of hiding your mess is a good thing. You should clean up your mess and if other people can help you with that, it is a good thing.

But, currently "Unobtrusive JavaScript" is getting more and more acceptance. And "Unobtrusive JavaScript" will hide a lot of you details but for good reasons:

  • Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation
  • Best practices to avoid the problems of traditional JavaScript programming (such as browser inconsistencies and lack of scalability)
  • Progressive enhancement to support user agents that may not support advanced JavaScript functionality

I find this presentation to be a nice one to get the idea and get you started.

5

Minimisation is your friend (I’d recommend Google’s Closure Compiler: https://code.google.com/closure/compiler/). This will:

  • remove white space
  • remove comments
  • minimise variable names
  • even goes so far as to do simple dead code analysis and removal

Of course, your best approach is to make sure you’re writing as good quality code as possible in the first place!

1
  • Absolutely, this question was more of a "I'm curious..." rather than "Help me..." :) Jan 31, 2011 at 12:19
4

No

JavaScript is presented client side right along with HTML and CSS. It's fully viewable by the client. No matter how much you compress and minimize it it will always be viewable. With tools like Firebug and Firequery inspecting the JavaScript hooks is even easier. I would not recommend investing time in hiding JavaScript.

1

I'm going to look at this from another point of view. Instead of trying to hide our dirty laundry, perhaps we should take the time to make sure what we push to our users is as clean as possible.

As you said, JavaScript runs in our customers browsers. This code is not parsed and passed to them from our servers, it's something that is run right on their machine and could potentially cause them some issues. Users have the option to say 'no I don't want to run JS, b/c I don't trust it.'

By trying to hide it away and make sure the user never sees it, how are we showing them that they can trust our code? How can we prove that what we are asking them to run isn't malicious?

As AJAX and jQuery become more and more popular, I feel that we are running out of excuses for having dirty laundry. These libraries make it nice and easy to provide a more advanced user experience. By taking some time, you can write very clean code that does some very powerful things.

At the end of the day if you are embarrassed about some JavaScript functions you are passing along, then clean them up. Taking time to hide it is a waste b/c if people really want to see it, an add-on will be made to allow them to.

1

Place your JS in separate files where possible. While you wont be able to prevent users who really want to see it from seeing it, you will prevent casual users from seeing most of the code when they 'view the page source'.

Of course, most users couldnt tell good JS from ancient Greek.

0

Sometimes a lot of client-side code can be moved server-side, depending on what you're doing. This is often the case when you need to hide something -- you generally don't want database access in your JavaScript, for example. Of course if your server is already straining then you may want to offload more work to the client, not less, but it can work in some cases.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.