I want to measure the size of text in JavaScript. So far itβs not so difficult, because I can just put a temporary invisible div in the DOM tree and check offsetWidth and offsetHeight. The problem is that I want to do this before the DOM is ready. Here is the stub:
<html> <head> <script type="text/javascript"> var text = "Hello world"; var fontFamily = "Arial"; var fontSize = 12; var size = measureText(text, fontSize, fontFamily); function measureText(text, fontSize, fontFamily) { </script> </head> <body> </body> </html>
Again: I KNOW how to do this asynchronously when the DOM (or body) signals that it is ready. But I want to do this synchronously right in the header, as shown in the header above. Any ideas how I can do this? My real opinion is that this is not possible, but maybe someone has a crazy idea.
source share