My JQuery script which does a lot of DOM manipulation works smoothly in Chrome (expected), runs very well in Safari and not so badly in Firefox.
Lets talk about internet explorer...
When I run a method that does a bit of DOM manipulation the following code causes the page to go white for about 1 second while it processes. The line that is effecting speed quite a lot is commented:
function AutoAssignImage(sourceImageDiv, destinationHolder) {
// Check nothing is assigned
if (!$(destinationHolder).has('.upload-pane-item').length) {
sourceImageDiv.find('.quickAssign').hide();
sourceImageDiv.find('.unassign').show();
sourceImageDiv.css({ border: "0px" });
$(destinationHolder).html(sourceImageDiv); // Causes blank screen while moves dom element
// Update Quick Assign buttons
updateQuickAssignButtons();
}
}
function updateQuickAssignButtons() {
quickAssign = "string gets generated here but is very quick";
$('#' + uploadPaneId + ' .quickAssignLinks').html(quickAssign); // Very slow
}
I need to update the HTML that is used on up to 500 elements. I have tried using a loop thinking it would update the first few elements almost instantly and could process the others while the user would not notice the slight delay. When I tried using a .each()
JQuery loop it didn't seem to make any different and still causes a white screen for about a second.
EDIT: The HTML that is commonly set is like the following:
<a class="assignLink" href="#">1</a>
<a class="assignLink" href="#">2</a>
<a class="assignLink" href="#">3</a>
<a class="assignLink" href="#">4</a>
<a class="assignLink" href="#">5</a>
<a class="assignLink" href="#">6</a><br />
<a class="assignLink" href="#">7</a>
<a class="assignLink" href="#">8</a>
<a class="assignLink" href="#">9</a>
<a class="assignLink" href="#">10</a>
<a class="assignLink" href="#">11</a>
<a class="assignLink" href="#">12</a>
Caching
I have tried storing the elements in a variable to enable some form of caching
var quickAssignElements; // top of js file
quickAssignElements = $('#' + uploadPaneId + ' .quickAssignLinks'); // called when DOM updates
$(quickAssignElements).html(quickAssign); // Calls this when it needs to update html on elements
This didn't seem to make a difference either.
Does anyone know of an alternative approach to minimise the delay / stop the window going blank?