473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

A way to stop redraws when modifying the DOM?

Hi,

I'm working on a web page that uses the DOM API to move elements within
the page in response to an onclick event. My problem is that in
Firefox (but not in Safari and IE) I can see everything move, almost
like an animation -- not what I want in this case. It appears that the
other browsers are not reflowing/redrawing the page until my event
handler returns. So my question: is there a function to explicitly
control this? Something like:

function handleClick() {
document.dontRedrawInResponseToDOMChanges();
... move 20 elements ...
document.okayNowYouCanRedrawAllAtOnce();
}

thanks,
Rob

Aug 11 '06 #1
2 1414
For you problem, I'd think that you're trying to sort table rows (or
div), am I right ? In any case, you should keep a separate array of the
elements you whish to have control over ; this will speed things up as
you won't have to do a document.getElement...(...); call all the time.
As this :

-------------- cut here ------------------------

<input type="button" onclick="sortedTable.sort(true);" value="Sort
(increase) !" />
<input type="button" onclick="sortedTable.sort(false);" value="Sort
(decrease) !" />

<table id="table">
<tr>
<td>1</td>
</tr>
</table>

<script language=javascript>

var MoveableRow = {};
MoveableRow = function( element ) {
this.element = element;
this.html = element.innerHTML;

// add whatever you like for properties... here, I set the "weight"
of the
// MoveableRow by the content of the first cell...
this.weight = parseInt( element.cells[0].innerHTML ); // make sure it
is an int
};
MoveableRow.prototype = {

getElement: function() {
return this.element;
},
getHTML: function() {
return this.html;
},
getWeight: function() {
return this.weight;
}

};

var SortableTable = {};
SortableTable = function( element ) {
// element = parent element to sort the childrens
this.element = element;
this.sortable = [];

// references all rows into an array
for ( var i=0; i<this.element.rows.length; i++ ) {
this.sortable.push( new MoveableRow( this.element.rows[i] ) );
};
};
SortableTable.prototype = {

sort: function( sortOption ) {
// your sort algorith on this.sortable here
// I just did a simple bubble sort in this case
for ( var i=0; i<this.sortable.length - 1; i++ ) {
for ( var j=i+1; j<this.sortable.length; j++ ) {
var e1 = this.sortable[i];
var e2 = this.sortable[j];
if ( (sortOption && e1.getWeight() e2.getWeight())
|| (!sortOption && e1.getWeight() < e2.getWeight()) ) {
this.sortable[j] = this.sortable[i];
this.sortable[i] = e2;
}
}
}

for ( var i=0; i<this.sortable.length; i++ ) {
this.element.rows[i].innerHTML = this.sortable[i].getHTML();
}
}

};

var table = document.getElementById('table');

// make sure we have a 1000 rows...
for (var i=table.rows.length; i<1000; i++) {
var row=table.insertRow(i);
row.innerHTML = "<td>" + (i+1) + "</td>";
}

var sortedTable = new SortableTable( table );

</script>
-------------- end of code --------------------------

Hope this helps.

Aug 12 '06 #2
Yanick wrote:
For you problem, I'd think that you're trying to sort table rows (or
div), am I right ?
Yes, I'm sorting table rows.
In any case, you should keep a separate array of the
elements you whish to have control over ; this will speed things up as
you won't have to do a document.getElement...(...); call all the time.
Thanks, this helped. I did have a call to getElementsByTagName()
inside the loop that was reinserting the elements. I removed this, and
the annoying animation effect disappeared. At first I was worried that
this was only because getElements...() was slow, and that the effect
would still exist on slower machines. But it appears the call to
getElement...() is what actually triggers the reflow in Firefox,
because this test code, which has an inner loop just to slow it down,
still works -- the new DOM structure appears in a flash, despite the
outer loop taking 2 seconds.

var body = document.getElementById("chapters");
var len = rows.length;
for (var i=0; i<len; i++) {
body.appendChild(rows[i]);
var sum;
for (var j=0; j<10000; j++) { sum += j; }
}

thanks,
Rob

Aug 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Japcuh | last post by:
How do you write self modifying code in Java? Japcuh (Just Another Perl C Unix Hacker) http://www.catb.org/~esr/faq/hacker-howto.htm#what_is ..0. ...0 000
2
by: rzed | last post by:
I am working with PythonCard in one of my apps. For its purposes, it uses an .ini file that is passed to ConfigParser. For my app, I also need configuration information, but for various reasons,...
2
by: Scott_From_PA | last post by:
I have a love hate thing going with Dreamweaver! One of the things I absolutely hate is the properties box when modifying form objects. Lets say you lay out a form in a table and it comes time...
8
by: Matt Theule | last post by:
While stepping through an ASP.NET project, I found that data was being inserted into my database even though I was not stepping through the code that inserted the data. I have a single page with...
6
by: ralphJake | last post by:
hi :} i have been looking for a script like this for a few weeks now, and i am finally giving up and asking for help. everything i found involved preventing overlap in dreamweaver, i want to...
7
by: Marc Bartsch | last post by:
Hi, I have a background worker in my C# app that makes a synchronous HttpWebRequest.GetResponse() call. The idea is to POST a file to a server on the internet. When I call HttpWebRequest.Abort()...
2
by: mark4asp | last post by:
Can I force the client to stop caching old stylesheets and javascript? In my dynamic web-site, I need to force the client to stop caching old versions of my stylesheets and javascript. Can I do...
5
by: Mahmoud Al-Qudsi | last post by:
Is there any way to stop an XmlDocument object from using data grabbed from a previous request? e.g. if I used XmlDocument.Load to grab a URI, and I know that this URI changes often (for...
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
0
by: Joe | last post by:
It sounds like you are using a WebSite Project. Look into a Web Application Project. My understanding is a WSP by default compiles by page and folder. a WAP can be configured to compile to a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.