i have code like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17115993-1']);
_gaq.push(['_trackPageview']);
(function() {
va开发者_运维百科r ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
does it matter at all, if put it all on one line? will there be any issues regarding how the browser interprets it>?
<script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-17115993-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script>
Nope, just readability.
It's the difference between a js file and one labeled .min (depending on who wrote it, of course).
The issue will be maintainability. Combining everything on a single line makes it so much more painful to read.
As far as functional operation of the code goes, there's no difference.
If you're concerned about size, your best bet is to keep the original code files multi-line and then use an automatic minify tool to create minified versions that you serve off of your production web server.
No, whitespace is not significant. Just be careful about missing semicolons:
Unlike C, whitespace in JavaScript source can directly impact semantics. Because of a technique called "semicolon insertion", some statements that are well formed when a newline is parsed will be considered complete (as if a semicolon were inserted just prior to the newline).
From: Wikipedia - JavaScript Syntax
In fact, most minification tools, such as the Google Closure Compiler, or the YUI Compressor, will remove whitespace as part of the minification.
Therefore, it is generally recommended to write your JavaScript code in a readable manner, and then use a minification tool to remove the whitespace. And always explicitly terminate your statements with a semicolon.
It should run the same all on one line. The advantage to multiple lines is that it is more human-readable. The browser won't care.
Also, minified files are often more difficult to debug. For example, you can't set a breakpoint in Firebug on a particular line, if everything's on one line.
It will all run on the same line, and the only real advantage of having it on one line is to save bandwidth, which for 99.9% of sites isn't a big enough saving to be of any concern. Your main concern should be readability.
If you are trying to make it unreadable, then your attempts will be futile as obfuscuation is a very weak form of security.
精彩评论