RESOLVED:
The line destination.block(…) is the one causing the errors. I’ve forcing empty to equal true to avoid this. I think that the if (empty) statement is just picking between two different ways of displaying a busy message.
I’m new to jQuery and have taken over maintaining an ASP.NET MVC 2 project from an ex-colleague. I’m struggling to understand what the following line of code is doing:
var empty = ($(":not(script)", destination).length == 0);
This line causes and error in IE but not in Firefox. To be honest looking at the function below I don’t think it’s needed, but as my colleague did not believe in commenting his code. I need to understand the impact of changing the code. The context for the above is the following Javascript function:
PRISMbase.LoadPartialEx = function(url, data, destination, successCallback, loadingMessage, appendNotOverwrite) {
var empty = ($(":not(script)", destination).length == 0);
var message = loadingMessage == null ? "Please wait" : loadingMessage;
if (empty)
destination.html(busy);
else
destination.block({ message: '<h1 class="Progress">' + message + '</h1>' });
return $.ajax({
data: data,
dataType: "html", type: "GET", url: url,
success: function(viewHtml) {
var partial = $(viewHtml);
if (!empty)
destination.unblock();
if(viewHtml != "")
{
if (appendNotOverwrite)
destination.append(partial);
else
destination.html(partial);
PRISMbase.InitialiseFormElements(destination);
}
else
destination.empty();
if (successCallback != null) successCallback(destination, partial);
}
});
}
In the above:
destination = a jQuery selector returning “#TabContent” (this is a div).
busy =’<img class="progress" src="/Content/OnyxProgressCircle.gif" />‘;
Any help would be appreciated.
Thanks.
Alan
That line of code is retrieving all of the non-script tags that exist inside of the destination selector and then testing for whether or not the number of found elements is 0. The variable
emptyshould end up as a Boolean value. How does the error read in IE?It might also be worth pointing out that an empty return from jQuery will evaluate false, so you could probably do something like this:
Be sure to notice the reversal in the order. In the original,
$(":not(script)", destination).length == 0givestruewhen there are no elements. In mine, we’re not storing that Boolean separately, we’re operating based on the initial return from jQuery, so no elements is thefalseresult.