Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  173] [ 2]  / answers: 1 / hits: 91360  / 11 Years ago, mon, april 8, 2013, 12:00:00

I need to accurately measure the dimensions of text within my web app, which I am achieving by creating an element (with relevant CSS classes), setting its innerHTML then adding it to the container using appendChild.



After doing this, there is a wait before the element has been rendered and its offsetWidth can be read to find out how wide the text is.



Currently, I'm using setTimeout(processText, 100) to wait until the render is complete.



Is there any callback I can listen to, or a more reliable way of telling when an element I have created has been rendered?


More From » dom

 Answers
38

There is currently no DOM event indicating that an element has been fully rendered (eg. attached CSS applied and drawn). This can make some DOM manipulation code return wrong or random results (like getting the height of an element).



Using setTimeout to give the browser some overhead for rendering is the simplest way. Using



setTimeout(function(){}, 0)


is perhaps the most practically accurate, as it puts your code at the end of the active browser event queue without any more delay - in other words your code is queued right after the render operation (and all other operations happening at the time).


[#79052] Saturday, April 6, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cristinadomoniquel

Total Points: 320
Total Questions: 94
Total Answers: 94

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
cristinadomoniquel questions
Wed, Apr 7, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Mon, Aug 17, 20, 00:00, 4 Years ago
;