【问题标题】:Ajax+Jquery Javascript running very slowly in Internet ExplorerAjax+Jquery Javascript 在 Internet Explorer 中运行非常缓慢
【发布时间】:2012-01-05 03:44:07
【问题描述】:

在一个网站上,我正在设计一个用户搜索城市中的酒店,结果以 xml 格式返回,然后通过 ajax/javascript/jquery 格式化。

结果在 Chrome 或 FF 中几乎立即显示,但在 Internet Explorer(我尝试过 ie6 和 ie9)中显示结果几乎需要一分钟。

在 IE 中运行开发人员工具表明 99% 的执行时间都花在了函数“getElementsByTagName”上,该函数被调用了近 200,000 次,但我不知道如果是这个问题该如何解决。

有问题的页面是beta.hotelsweep.com,搜索时调用的函数是:

//summarized version of the function
$.get(url, function (xmlResponse) {
    $('#results').empty();
    var exception = $("Exception", xmlResponse);
    if (exception.size() > 0) {
        var error = "<h2>We were unable to complete your request</h2>";
        $('#results').html(error);
    } else {
        $('#numResults').html($("resultsNumber", xmlResponse).text() + " hotels found <br>");
        var resultsHtml = "<div id='results_list'>";
        //set googlem map to center
        map.setCenter(new GLatLng($("avgLat", xmlResponse).text(), $("avgLong", xmlResponse).text()), 9);

    // Loop through response, creating <li> for each hotel
        $("Hotel", xmlResponse).each(function () {

            var bookLink = $('affiliateLink', this).text();
            var address = $('fulladdress', this).text();
            var stars = $('stars', this).text();
           resultsHtml += 'Hotel Stars: ' + stars;
        });

    //put html into results div
        $('#results').html(resultsHtml);
    }
});

【问题讨论】:

  • Chrome 使用的 javascript 引擎比 IE 快得多。

标签: javascript jquery ajax performance internet-explorer


【解决方案1】:

您无法真正解决此问题。 getElementsByTagName 在旧版本的 IE 中没有索引。您可以改用元素 id 查找(避免使用 $("Exception")、$("avgLat") 等)。

如果这不可行,您可以在服务器上添加一个脚本以将结果呈现为 HTML。

【讨论】:

  • 您能否链接您从中学到的任何资源,getElementsByTagName 在旧版本的 IE 中没有索引?听起来很奇怪。
【解决方案2】:

原来问题与文档模式有关。在 quirk 模式下,javascrit 甚至没有完全执行。

将此添加到顶部:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<meta http-equiv="content-type" content="text/html;charset=utf-8"> 

 <meta http-equiv="X-UA-Compatible" content="IE=9" />

使 Internet Explorer 按预期执行。我不完全理解发生了什么,但如果您发现您的 javascript 仅在 IE 中执行不正确,则可能与文档模式有关。

【讨论】:

  • 我明天会,点击接受勾号告诉我“明天你可以接受自己的答案”
猜你喜欢
  • 2017-03-20
  • 2018-08-30
  • 2014-11-10
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
相关资源
最近更新 更多