473,503 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining string length in pixels

I am adapting a javascript pulldown menu system to my dynamic website
generator - the arrays that hold the menu items information are read from a
database and will be different for different users of my system.
Unfortunately the javascript menu system requires that each menu cell's
width be dimensioned. if I don't hard code a large enough value then long
menu item names are trimmed, besides the fact that it then looks ugly for
most menu items that are much smaller than the hardcoded width. What I would
like to do is find the actual pixel width of a string which will have to
take into account the font, the character size, bold on or off, number of
characters and of course proportional font or not will make a difference
too...is there a way of determining in advance how wide in pixels a string
is going to be? e.g. if I set it to some hidden named html element with all
of its style properties, could I then retrieve the width of that element
with javascript? Thanks!
Jul 20 '05 #1
1 26873
Simon Wigzell wrote:
I am adapting a javascript pulldown menu system to my dynamic website
generator - the arrays that hold the menu items information are read from a
database and will be different for different users of my system.
Unfortunately the javascript menu system requires that each menu cell's
width be dimensioned. if I don't hard code a large enough value then long
menu item names are trimmed, besides the fact that it then looks ugly for
most menu items that are much smaller than the hardcoded width. What I would
like to do is find the actual pixel width of a string which will have to
take into account the font, the character size, bold on or off, number of
characters and of course proportional font or not will make a difference
too...is there a way of determining in advance how wide in pixels a string
is going to be? e.g. if I set it to some hidden named html element with all
of its style properties, could I then retrieve the width of that element
with javascript? Thanks!


<body onload="test('some text');test('some longer text');">
<script type="text/javascript">
function test(s) {
if (document.getElementById) {
var rulerSpan = document.getElementById('ruler');

rulerSpan.innerHTML = s;

alert(rulerSpan.offsetWidth);
}
}
</script>

<span id="ruler" style="visibility:hidden;"></span>

You'd class the span the same as the menu items so they'd inherit the font
family/size/etc.

It should work in IE 5.5+, Netscape 6 and Opera 7+.

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
14610
by: Mitchua | last post by:
Simplified a bit, I'm parsing HTML documents to get sentences e.g. my $html = get($URL); # remove all HTML TAGs...blah blah blah @sentences = split(/\./, $html)); then I'm trying to determine the...
18
5587
by: Zygmunt Krynicki | last post by:
Hello I've browsed the FAQ but apparently it lacks any questions concenring wide character strings. I'd like to calculate the length of a multibyte string without converting the whole string. ...
8
1791
by: Al Sav | last post by:
Hi, Which one of these below two is a better way to find out if the string is of zero length? string.Length == 0 or string.Length < 0 Alwin S.
6
4248
by: Cathie Hagen | last post by:
Hi All, I am trying to work out how to find the string-length in pixels (any units would do at this stage) for my document so I can determine column width. Any ideas? Cathie
1
1294
by: MattB | last post by:
Hi. I've got a vb.net/asp.net application that passes strings back a forth between a c++ COM object. We use simple tagging to designate what's what, and it's worked pretty well until now. We had...
2
8170
by: Brad | last post by:
hello all, I am trying to "draw" a string onto a graphic Dim pGraphics As System.Drawing.Graphics pGraphics.DrawString(TheText, drawFont, drawBrush, 100, 100) but I need to find out the...
3
7400
by: ipellew | last post by:
Hi; Whats the maximum string length in Javascript. Is it the same across all the browsers? max_s_len = str.length // what the max number of chars we can have is str? Regards
9
7068
by: zion | last post by:
Hello, In my program I get data from table with 2 fields and enter it like in example into string array. How can I add the right spaces to the string? I use the function...
6
6125
by: csmith8933 | last post by:
I'm trying to loop until the string length is 0, deleting one character at a time. This doesnt work for obvious reasons I'm sure. string s; s = "0000011111"; while (s.length() 0) {...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.