I've been tweaking this and that and the other in an attempt to get the "Stop running this script?" error to go away in IE7 - the browser all users are required to use at this time :P I've tried several improvement attempts; all of them have caused the script to stop working, instead of running long. I've tried using setTimeout(), with no luck. It's possible I didn't do it correctly. Can anyone suggest ways to improve this to make it more efficient (and get that dang long running script message to go away)?
Here's the code:
The HTML:
<div class="changeView" style="float:right;">Show All...</div>
<div id="accordion" style="width: 99%;">
<% foreach (var obj in Model.Objects) { %>
<h3><a href="#"><span class="title"><%:obj.Id%></span><span class="status" style="font-size:75%"> - <%:obj.Status%></span></a></h3>
<div id="<%:obj.Id %>">
<div class="loading"><img src="<%=Url.Content("~/Content/Images/ajaxLoader.gif") %>" alt="loading..." /></div>
</div>
<% } %>
</div>
Then we have an onclick function to start it off...
$(function () {
$(".changeView").click(function () {
var divText = $(this).html();
var src = '<%=Url.Content("~/Content/Images/ajax-loader.gif")%>';
if (divText == "Show All...") {
$(this).html("Show Filtered...");
$('#accordion').accordion('destroy');
$('#accordion').empty();
$('#accordion').addClass("loading");
$('#accordion').append('Loading Information...<img src="' + src + '" alt="loading..." />');
changePartialView("all");
}
else {
$(this).html("Show All...");
$('#accordion').accordion('destroy');
$('#accordion').empty();
$('#accordion').addClass("loading");
$('#accordion').append('Loading Information...<img src="' + src + '" alt="loading..." />');
changePartialView("filter");
}
});
});
Next the changeView function is called:
//change view and reinit accordion
function changePartialView(viewType) {
$.ajax({
type: "POST",
url: "<%:Model.BaseUrl%>" + "ToggleView",
data: "Type=<%:Model.Target%>&Id=<%:Model.Id%>&view=" + viewType,
success: function (result) {
$('#accordion').empty();
$('#accordion').removeClass();
for (var index = 0; index < result.Html.length; index++) {
$('#accordion').append(result.Html[index]);
}
var $acc = $("#accordion").accordion({
collapsible: true,
active: false,
autoHeight: false,
change: function (event, ui) {
var index = $acc.accordion("option", "active");
if (index >= 0) {
var clickedId = ui.newHeader.find("a").find(".title").text();
getRequirements(clickedId);
}
else {
// all panels are closed
}
}
});
},
error: function (xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
alert("Error in ajax: " + result);
}
});
}
Note: The result.Html returns a generic List of formatted HTML, one for each panel of the accordion. With the exception of the long running script error message, everyone works pretty sweet.
Clarification of returned value: The result.Html consists of about 200-250 instances of these strings:
"<h3><a href=\"#\"><span class=\"title\">" + obj.Id +
"</span><span class=\"status\" style=\"font-size:75%\"> - " + obj.Status + count +
"</span></a></h3><div id=\"" + obj.Id + "\"><div class=\"loading\"><img src=\"" +
Url.Content("~/Content/Images/ajaxLoader.gif") + "\" alt=\"loading...\" /></div></div>")
<div>element before the loop that appends the ajax results, append the results to that instead of the real DOM, and then append the<div>when finished.result.Html? I'm wondering if you are appending a long string, character by character...alert(result.Html.length)and see what IE7 says as compared to other browsers.