我有一个jsp,它使用了大量的javascript,但速度不够快。我想首先对其进行优化,这是代码的一部分:
在jsp中我进行了初始化:
window.onload = function () {
formCollection.pageSize.value = "<%= pagingSize%>";
elemCollection = iDom3.Table.all["spis"].XML.DOM;
<% if (resultList != null) { %>
elementsNumber = <%= resultList.size() %>;
<%} else { %>
elementsNumber = 0;
<% } %>
contextPath = "<%= request.getContextPath() %>";
}
在我的 js 文件中,我有两种类型的 js 函数:
// gets the first element and sets it's value to all the other;
//the selectSingleNode function is used because I use XSLT transformation
//to generate the table
_setTehJed = function(){
var resultId = formCollection.elements["idTehJedinice_spis_1"].value;
var resultText = formCollection.elements["tehnicka_spis_1"].value;
if (resultId != ""){
var counter = 1;
while (counter<elementsNumber){
counter++;
if(formCollection.elements["idTehJedinice_spis_"+counter] != null){
formCollection.elements["idTehJedinice_spis_"+counter].value=resultId;
formCollection.elements["tehnicka_spis_"+counter].value=resultText;
}
var node=elemCollection.selectSingleNode("/suite/table/rows/row[@id = 'spis_"+counter+"']/data[@col = 'tehnicka']/title");
node.text=resultText;
var node2=elemCollection.selectSingleNode("/suite/table/rows/row[@id = 'spis_"+counter+"']/data[@col = 'idTehJedinice']/title");
node2.text=resultId;
}
}
}
// sets the elements checkbox to checked or unchecked
_SelectCheckRokCuvanja = {
all : [],
Item : function (oItem, sId) {
this.all["spis_"+sId] = oItem.value;
if (oItem.checked) {
elemCollection.selectSingleNode("/suite/table/rows/row[@id = 'spis_"+sId+"']/data[@col = 'rokCheck']").setAttribute("default", "true");
}else{
elemCollection.selectSingleNode("/suite/table/rows/row[@id = 'spis_"+sId+"']/data[@col = 'rokCheck']").setAttribute("default", "false");
}
}
}
我使用了这些技巧:
- https://web.archive.org/web/20151029015752/http://blogs.msdn.com:80/b/ie/archive/2006/08/28/728654.aspx
- http://code.google.com/speed/articles/optimizing-javascript.html
但我仍然认为可以做一些事情,比如定义这样的函数:
在jsp中:
window.onload = function () {
iDom3.DigitalnaArhivaPrihvat.formCollection=document.forms["controller"];
iDom3.DigitalnaArhivaPrihvat.formCollection.pageSize.value = "<%= pagingSize%>";
iDom3.DigitalnaArhivaPrihvat.elemCollection = iDom3.Table.all["spis"].XML.DOM;
<% if (resultList != null) { %>
iDom3.DigitalnaArhivaPrihvat.elementsNumber = <%= resultList.size() %>
<%} else { %>
iDom3.DigitalnaArhivaPrihvat.elementsNumber = 0;
<% } %>
}
在js中:
iDom3.DigitalnaArhivaPrihvat = {
formCollection:null,
elemCollection:null,
elementsNumber:null,
_setTehJed : function(){
var resultId = this.formCollection.elements.idTehJedinice_spis_1.value;
var resultText = this.formCollection.elements.tehnicka_spis_1.value;
if (resultId != ""){
var counter = 1;
while (counter<this.elementsNumber){
counter++;
if(this.formCollection.elements["idTehJedinice_spis_"+counter] !== null){
this.formCollection.elements["idTehJedinice_spis_"+counter].value=resultId;
this.formCollection.elements["tehnicka_spis_"+counter].value=resultText;
}
var node=this.elemCollection.selectSingleNode("/suite/table/rows/row[@id = 'spis_"+counter+"']/data[@col = 'tehnicka']/title");
node.text=resultText;
var node2=this.elemCollection.selectSingleNode("/suite/table/rows/row[@id = 'spis_"+counter+"']/data[@col = 'idTehJedinice']/title");
node2.text=resultId;
}
}
},
_SelectCheckRokCuvanja = {
all : [],
Item : function (oItem, sId) {
this.all["spis_"+sId] = oItem.value;
if (oItem.checked) {
this.elemCollection.selectSingleNode("/suite/table/rows/row[@id = 'spis_"+sId+"']/data[@col = 'rokCheck']").setAttribute("default", "true");
}else{
this.elemCollection.selectSingleNode("/suite/table/rows/row[@id = 'spis_"+sId+"']/data[@col = 'rokCheck']").setAttribute("default", "false");
}
}
}
但问题在于范围界定(如果我这样做,第二个函数将无法正确执行)。有什么建议吗?
编辑:表的数据结构:
<row id="spis_3">
<data col="rokCheck" type="check" default="false" onchange="_ChangeTypeRokCuvanje(this,'3')" >
<title>false</title>
<description>Tehnička jedinica</description>
</data>
最佳答案
selectSingleNode()
是一个相当昂贵的操作。你应该尽量避免它。解决办法有两种:
自己迭代数据结构(这样您就不必在 DOM 中搜索表,而是可以直接逐行修改)。
由于您为每一行分配一个 ID,因此您应该能够使用
document.getElementById()
,而无需昂贵的 XPath 查找。使用 jquery 之类的框架一次获取所有相关节点(-> 仅 1 次昂贵的查找),然后迭代列表。
[编辑]参见Traversing an HTML table with JavaScript and DOM Interfaces有关如何实现 #1 的示例。
关于javascript - 如何优化这个 JSP/JavaScript 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2927789/