JavaScript
- JavaScript 要放到html底部,是因为浏览器遇到一个< script >,将会中断该网页上所有进一步的呈现.—-高性能PHP应用开发
下面是两个测试的代码(我自己测试没测试出书上的效果,不过可以了解下)
<html>
<head>
<title>JavaScript Example</title>
<script type="text/javascript">
function addItems(){
var items = ['apache','nginx','lighty'];
localDoc = document;
var node = '',i=0,li = '';
node = localDoc.getElementById('webservers');
for(i;i<items.length;i++){
li = document.createElement('li');
li.innerHTML = items[i];
node.appendChild(li);
}
}
</script>
</head>
<body>
<div id="main">
<h3>Technology List</h3>
<ul id="techlist">
<li>Javascript</li>
<li>Css</li>
<li>Images</li>
<li>PHP</li>
</ul>
<ul id="webservers"></ul>
</div>
<script type="text/javascript">
addItems();
</script>
</body>
</html>
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<div id="main">
<h3>Technology List</h3>
<ul id="techlist">
<li>Javascript</li>
<li>Css</li>
<li>Images</li>
<li>PHP</li>
</ul>
<ul id="webservers"></ul>
</div>
<script type="text/javascript">
function addItems(){
var items = ['apache','nginx','lighty'];
localDoc = document;
var node = '',i=0,li = '';
node = localDoc.getElementById('webservers');
for(i;i<items.length;i++){
li = document.createElement('li');
li.innerHTML = items[i];
node.appendChild(li);
}
}
addItems();
</script>
</body>
</html>
- 精简JavaScript
删除空白空间,去除注释等等
YUI Compressor官网
YUI Compressor
//-o 指定输出的文件位置
java -jar yuicompressor-x.y.z.jar myfile.js -o myfile-min.js
参考
高性能PHP应用开发