简体   繁体   中英

How can I shorten the lines in javascript?

I've got the google analytics javascript and I want to make it smaller. But I thought that you couldn't just put an enter somewhere... So where CAN I start a new line in this code?

var _gaq = _gaq || []; _gaq.push(['_setAccount', 'secret']); _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);
  })();

EDIT: Why? Because my screen is to small. It's for readability.

EDIT2: What about this approach? (The use of a '\\')

 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : '\
 http://www') + '.google-analytics.com/ga.js';

I'm really not sure why you want to do this, nor would I recommend it, but here you go.

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'secret']);
_gaq.push(['_trackPageview']);
(function() {
  var ga = document.createElement('script');
  ga.type = 'text/javascript'; ga.async = true;
  var start;
  if ('https:' == document.location.protocol) {
    start = 'https://ssl';
  } else {
    start = 'http://www';
  }
  ga.src = start + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(ga, s);
})();

尝试在线YUI压缩程序,它适用于javascript和CSS。

You can start a new line after each semi-colon.

Before:

var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

After:

var ga = document.createElement('script'); 
ga.type = 'text/javascript'; 
ga.async = true;

To my eye, this makes it much easier to read. I wouldn't necessarily keep it this way when I deploy it, but you could.

In addition to being easier to read, this makes it easier to step through line by line if you are debugging.

after any ';'

var _gaq = _gaq || []; 
_gaq.push(['_setAccount', 'secret']);
_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);
})();

But why do you want to do that?

You can do it manually after every semicolon, but you can also do it automatically. Try entering "javascript formatter" into Google and you get Online JavaScript Beautifier for example.

This is the code after "beautification":

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'secret']);
_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);
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM