Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Source code: general web development question for which I have not been able to find an answer

Why is the source code of web pages available to everyone to see? I am referring to HTML, CSS files, JavaScript code. All of this can be obtained from any website.

In contrast, applications that target operating systems rarely offer the source code for everyone to see. I have always wondered. I will appreciate an answer and the effort taken to respond.

7 Answers

Not sure it's the best technical answer but I'll give it a try. source-code from applications used in operating systems usually needs to be compiled into a binary or executable file. Source-code from the websites frontend are usually markup, CSS, JS, etc which are just read and interpreted by the browser. I wouldn't stress with this too much.

I have wondered this myself before.

I wouldn't worry about it too much. There is nothing ever exciting in the HTML, CSS, or JS. The HTML and CSS of a site are usually what you expect it to be before you even view it. A lot of the JS are just plugins that anybody can get or use from github.

Custom JS is almost meaningless unless you bring in the same html and css markup that JS is manipulating.

You shouldn't have sensitive information in that information anyway. In contrast to being able to view the browser side languages of those 3, you don't get to see the server side like PHP or Python, which very likely dynamically generated the HTML mark up you can view. Those languages is where you would put your database info, do your connects, queries, etc....and that information shouldn't display to the browser unless you have a security issue.

im not sure why its viewable but im glad it is say you see a fancy layout you can peek under the hood and get a feel for how it works

if i was to give a answer id say it is because is html is sent to the browser as html and the browser reads it and devs thought hey why not make it viewable to the end user cause then web devs can go into view source to try and find issues with there apps or sites by seeing how the source is running

whereas programs are compiled into a unreadable form for security also because they are converted down to a lower level programing language

a example is

hello \/ print "hello"; \/ sys pros display string view 1 "hello(most likeley in ascii)" \/ the above but in assembly instructions \/ a whole mess of 1's and 1s

so yeah as you go lower and lower in the program language it gets less human readable so partly copyright partly you cant read computer language

also as mentioned almost nothing of worth is done user side so seeing it isnt a big deal the real money and secrecy stuff is server side where the grunt labour is done

Thank you for your responses, guys. You have cleared my doubt.

now ive thought about it i think theres actually a htaccess rule to hide javascript from being opened by the viewer if you store it externally im not sure ive never done it but in theory disabling hot linking on js files would do that

If you wanted to protect your javascript for any reason one idea is to find an automated tool that will "minify" your code. If you use Jquery at all you'll know you can link to a "jquery-1.7.js" or "jquery-1.7-min.js" or something like that. Basically both of these do the exact same thing, but the -min.js version has all the nice spacing and indentation and probably code comments removed and everything is squashed together and impossible to read. The idea is to streamline the file for faster loading and, yes, make it REALLY hard to figure out what's going on when you're looking at it.

As to why you can't see the other background languages, ya pretty much what everyone said here. The things your browser has to process (HTML, CSS, JS) are visible because the processing doesn't happen until it gets there. However scripts written in Perl or PHP are server-side languages and get processed before anything gets sent to your browser, usually these scripts are what assemble the html and other stuff in the first place.

If you wanted to protect your javascript for any reason one idea is to find an automated tool that will "minify" your code.

That's actually pretty useless idea for protecting JavaScript, as there are plenty of sites such as http://jsbeautifier.org/ that will de-minify JavaScript code.