473,605 Members | 2,703 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 26916
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('s ome text');test('so me longer text');">
<script type="text/javascript">
function test(s) {
if (document.getEl ementById) {
var rulerSpan = document.getEle mentById('ruler ');

rulerSpan.inner HTML = s;

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

<span id="ruler" style="visibili ty: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*****@agrico reunited.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
14617
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 number of characters in the sentence. However, although when I print the sentences they look fine, when I use length($sentence) I get values in the hundreds for small sentences. Most documentation I found said "length() returns the number of...
18
5601
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. Zygmunt PS: The whole multibyte string vs wide character string concept is broken IMHO since it allows wchar_t not to be large enough to contain a full character (rendering both types virtually the same). What's the point of
8
1798
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
4253
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
1302
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 some data that wasn't parsing right because it also contained tags that conflicted with the tags designating the data. To get around this the programmer of the COM object (not me) decided we could put string lengths in the tags, so if an element...
2
8179
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 Length of the string in pixels so that I can auto place it correctly in the picture (instead of using 100,100).
3
7409
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
7078
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 mystring.Tostring().PadLeft(10,' ') but the result is not good. 1234567890 | ABCDEFG
6
6133
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) { functionOne(s); }
0
8001
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7934
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8415
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6742
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3912
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.