Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

Determine width of a piece of text
Welcome, Guest. Please login or register.
December 01, 2008, 08:21:33 PM
11304 Posts in 1248 Topics by 496 Members
Latest Member: teentiodo
Experts Round Table Network  |  Serverside Technology  |  ASP  |  Determine width of a piece of text « previous next »
Pages: [1]
Author Topic: Determine width of a piece of text  (Read 712 times)
NeoTeq

Offline Offline

Posts: 21


« on: January 22, 2006, 05:57:15 AM »

I've been busy creating some ASP code that creates SVG graphs from data and options supplied by the querystring. The idea is that I can create some basic graphs dynamically for use on the intranet, so in my HTML page I would include an object:

Code:

<object data="graph.asp?width=500&height=300&data=10:14:11:7&labels=2001:2002:2003:2004" width="300" height="100"
type="image/svg+xml"
codebase="http://www.adobe.com/svg/viewer/install/" />


The problem I'm facing now is, to know how much room I will need for my labels I need to determine the width needed for the biggest label.

What I know is:
    - The font face
    - The font size
    - The label text


I'm using javascript as a server-side language. Can this be done? If there's a way to make an educated guess I'll gladly accept it as well.
Logged

Still claiming: There is no peace.
Srirangan
Mentor

Offline Offline

Posts: 52



WWW
« Reply #1 on: January 22, 2006, 06:20:13 AM »

With some client side javascript you can get the screen width. You can set this value into a cookie, and read the cookie from your server side program. Just a thought.

- Srirangan
Logged

I rant therefore I am!
COBOLdinosaur
ERT.com Admin

Offline Offline

Posts: 481



WWW
« Reply #2 on: January 22, 2006, 06:46:23 AM »

The problem is that aside form the fact that the users setting will override your font settings, you don't know definitely what fonts they have installed, you don't know what platform they are on and yo udo not know the rendering characteristics for their monitor.

You have to get the sizing information from the client.  Here is the easy way using client side Javascript:

Put the text in a span with an id.
Code:
<span id="sizer">your text here</span>


Then just query the properties:

Code:
alert(document.getElementById('sizer').offsetWidth);


You have to wait for the body to load.  If you need it on the user initial entry to the site.  You could give them an index page with nothing on it but that with the text set the same color as teh background and then set the cookie with the value and re-direct to your real page. If they are going to be coming through another page you can capture from any of th eother pages and save it to the cookie.
Logged
NeoTeq

Offline Offline

Posts: 21


« Reply #3 on: January 22, 2006, 06:49:11 AM »

Doing it client-side is an idea. I could even determine the text-width on the HTML page calling the ASP. That way I wouldn't even need to use cookies.

Ofcourse, I do still prefer server-side. But this will work for sure. Thanks!
Logged

Still claiming: There is no peace.
Pages: [1]
« previous next »
    Jump to: