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?
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.
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.
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.
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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With