473,386 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 26844
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.