优化 IE6 的字符串代码
我的网站上有一个页面,它使用 JS 通过字符串写出大量动态内容,然后将其解析为 HTML。在所有浏览器中都可以正常工作,但 IE6 非常慢。许多用户最终都会收到“脚本无响应,您想中止吗?”消息。
我尝试过使用数组而不是字符串来查看 IE6 是否可以更好地处理这些问题,但我仍然获得相同的性能。我想知道是否有人对如何针对 IE6 进行优化有什么聪明的想法,或者以其他方式防止出现无响应的脚本消息。
function createTable(){
var tableStr = "<table><tbody>";
tableStr += "</tbody></table>";
for(var x=0; x<contentData.length;x++){
tableStr += createRow(contentData[x]);
}
$("#content").html(tableStr);
}
function createRow(data){
var rowStr = "<tr>";
rowStr += "<td>" + data.name + "</td>";
rowStr += "<td>" + data.address + "</td>";
rowStr += "<td>" + data.phone + "</td>";
rowStr += "<td>" + data.fax + "</td>";
rowStr += "</tr>";
return rowStr;
}
I have a page on my site that uses JS to write out a lot of dynamic content via strings that are then parsed as HTML. Works great in all browsers, but IE6 is incredibly slow. Many users end up getting the "script is unresponsive, would you like to abort?" message.
I've tried using arrays instead of strings to see if IE6 handles those better, but I still get about the same performance. I was wondering if anyone had any clever ideas on how this can be optimized for IE6, or otherwise prevent that unresponsive script message from appearing.
function createTable(){
var tableStr = "<table><tbody>";
tableStr += "</tbody></table>";
for(var x=0; x<contentData.length;x++){
tableStr += createRow(contentData[x]);
}
$("#content").html(tableStr);
}
function createRow(data){
var rowStr = "<tr>";
rowStr += "<td>" + data.name + "</td>";
rowStr += "<td>" + data.address + "</td>";
rowStr += "<td>" + data.phone + "</td>";
rowStr += "<td>" + data.fax + "</td>";
rowStr += "</tr>";
return rowStr;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请查看这篇文章,其中包含有关如何改进的建议IE 的 JavaScript 性能
Look this post with recommendations on how to improve Javascript performance for IE
最佳解决方案是将用户引导至 http://www.ie6countdown.com/ - 由以下人员创建的网站微软将让世界摆脱 IE6。我认为当一家公司恳求客户停止使用他们的产品时,这说明了很多问题。
但你可能没有资格这样做。在 IE6 中解决这种情况的最佳解决方案是在服务器上构建 HTML;字符串操作和分配给
innerHTML
在 IE6 上非常慢,并且没有“技巧”可以使它更快。给您一个想法:我必须显示 2MB 的 HTML。 IE6 在不到一秒的时间内从磁盘加载了该文件。通过指定
innerHTML
将相同的 HTML 添加到现有页面需要 110 秒。或者您可以使用计时器拆分添加以避免阻塞页面。请参阅此答案:设置大值时提高性能的方法到innerHTML
The best solution is to point users to http://www.ie6countdown.com/ - a site created by Microsoft to rid the world of IE6. I think it says a lot when a company begs customers to stop using one of their products.
But you're probably in no position to do this. The best solution to solve this kind of scenario in IE6 is to build the HTML on the server; string operations and assigning to
innerHTML
is very slow on IE6 and there are no "tricks" to make it faster.To give you an idea: I had to display 2MB of HTML. IE6 loaded that from disk in less than one second. Adding the same HTML to an existing page by assigning to
innerHTML
took 110 seconds.Or you can split the adding using a timer to avoid blocking the page. See this answer: Ways to increase performance when set big value to innerHTML
您是否考虑过使用模板引擎?有多个可用于 jQuery/javascript 的模板引擎在这种情况下可能会有所帮助。
查看此堆栈溢出问题
Have you considered using a templating engine? There are multiple templating engines available for jQuery/javascript that may help in this situation.
Check out this stack overflow question