Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing a DOM refresh in Internet explorer after javascript dom manipulation

Here is the situation. I have some javascript that looks like this:

function onSubmit() {     doSomeStuff();     someSpan.style.display="block";     otherSpan.style.display="none";     return doLongRunningOperation; } 

When I make this a form submit action, and run it from a non IE browser, it quickly swaps the two spans visibility and run the long javascript operation. If I do this in IE it does not do the swap until after onSubmit() completely returns.

I can force a dom redraw by sticking an alert box in like so:

function onSubmit() {     doSomeStuff();     someSpan.style.display="block";     otherSpan.style.display="none";     alert("refresh forced");     return doLongRunningOperation; } 

Also, the obvious jquery refactoring does not affect the IE behavior:

function onSubmit() {     doSomeStuff();     $("#someSpan").show();     $("#otherSpan").hide();     return doLongRunningOperation; } 

This behavior exists on IE8 and IE6. Is there anyway to force a redraw of the DOM in these browsers?

like image 698
Justin Dearing Avatar asked Sep 09 '09 04:09

Justin Dearing


People also ask

How do you refresh a DOM element?

The location reload() method in HTML DOM is used to reload the current document. This method refreshes the current documents. It is similar to the refresh button in the browser.

Does JavaScript manipulate the DOM?

The DOM stands for Document Object Model. It can simply be understood as a tree of nodes created by the browser. Each of these nodes has its own properties and methods which can be manipulated using JavaScript. The ability to manipulate the DOM is one of the most unique and useful abilities of JavaScript.

Does innerHTML cause reflow?

innerHTML will only trigger a reflow when setting it changes the DOM. offsetParent shouldn't do anything, getting it might flush the render tree queue. style might trigger reflow and repaint when setting it (or properties of it) or chain those actions. Some properties like color should only trigger repaints.


2 Answers

Mozilla (maybe IE as well) will cache/delay executing changes to the DOM which affect display, so that it can calculate all the changes at once instead of repeatedly after each and every statement.

To force an update (to force an immediate, synchronous reflow or relayout), your javascript should read a property that's affected by the change, e.g. the location of someSpan and otherSpan.

(This Mozilla implementation detail is mentioned in the video Faster HTML and CSS: Layout Engine Internals for Web Developers.)

like image 74
ChrisW Avatar answered Oct 06 '22 00:10

ChrisW


To continue what ChrisW says:

here's flushing script to flash DOM, so you don't have to call alert(""); (found at http://amolnw.wordpress.com/category/programming/javascript/):

function flushThis(id){    var msie = 'Microsoft Internet Explorer';    var tmp = 0;    var elementOnShow = document.getElementById(id);    if (navigator.appName == msie){       tmp = elementOnShow.parentNode.offsetTop  +  'px';    }else{       tmp = elementOnShow.offsetTop;    } } 

It works for me!!! Thanks for the tip.

like image 21
OKL Avatar answered Oct 06 '22 00:10

OKL