admin管理员组文章数量:1357615
i have code like this:
<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/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/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script>
i have code like this:
<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./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./ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script>
Share
Improve this question
edited Jun 24, 2010 at 21:37
Alex Gordon
asked Jun 23, 2010 at 22:16
Alex GordonAlex Gordon
61k304 gold badges705 silver badges1.1k bronze badges
6 Answers
Reset to default 3Nope, 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 plete (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 remended 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.
本文标签: htmljavascript what is the advantage between multiline and singlelineStack Overflow
版权声明:本文标题:html - javascript: what is the advantage between multi - line and single - line? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744077729a2587058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论