Jam*_*mes 0 javascript performance asynchronous
我不是一个巨大的JavaScript性能大师.简单地想知道,我可以将以下代码更紧凑吗?不像打包或压缩它,而是按照它的编写方式.
(function() {
var jq = document.createElement('script');
var an = document.createElement('script');
var cm = document.createElement('script');
var ga = document.createElement('script');
var domain = 'http://example.com/';
jq.src = domain + 'jquery.1.3.2.js';
an.src = domain + 'jquery.alphanumeric.js';
cm.src = domain + 'common.js';
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
ga.setAttribute('async', 'true');
document.documentElement.firstChild.appendChild(jq);
document.documentElement.firstChild.appendChild(cm);
document.documentElement.firstChild.appendChild(an);
document.documentElement.firstChild.appendChild(ga);
})();
Run Code Online (Sandbox Code Playgroud)
干杯啦!
写作方式的紧凑性和性能是无关的.但要以更紧凑,可重复使用的方式编写它:
function appendScript(url, async) {
var el = document.createElement('script'),
root = document.documentElement;
el.async = async;
el.src = url;
// avoid an IE6 bug by using insertBefore (http://bugs.jquery.com/ticket/2709)
root.insertBefore(el, root.firstChild);
}
appendScript('http://example.com/js/jquery.1.3.2.js', false);
appendScript('http://example.com/js/jquery.alphanumeric.js', false);
appendScript('http://example.com/js/common.js', false);
appendScript(('https:' == document.location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js'), true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1452 次 |
| 最近记录: |