cliping a string without loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ofer h

    #1

    cliping a string without loops

    hello all
    i have got a span inside a td of a table. i am trying to clip the
    string that exceeds the length of the td and then put three dots
    ("...") after it so it will show that the text was clipped and when
    the mouse hovers above it, it will show the title. now i have managed
    to do all the above by puting a span inside the td with the style
    overflow:hidden and it all works fine. to add the three dots i have to
    clip the string to ten pixels below the length of the td and then add
    the dots. i can do it by slicing the string in "string.len gth-1" each
    time and then get the new length of the string with offsetWidth, and
    when getting to the right length - add the dots. the problem is that
    the slicing is done in a "while" loop, and when having a very long
    table it takes too much time. the question is if there is any methods
    of clipping the string by pixels or any other method that will shorten
    the run time of the script.

    p.s.
    i tried also having another span with the three dots that is
    display:none and when the length exceeds the span length (the length
    of the span is set to the td-10px) i change the display to block, but
    the second span keeps on droping a line which is not good for me

    thank you very much
    ofer harari @ oferh@yaelsoft. com
  • kaeli

    #2
    Re: cliping a string without loops

    In article <e067708.040105 0738.6ca21b33@p osting.google.c om>,
    oferh@yaelsoft. com enlightened us with...[color=blue]
    > overflow:hidden and it all works fine. to add the three dots i have to
    > clip the string to ten pixels below the length of the td and then add
    > the dots.[/color]

    Um, that won't work very well if a user changes the resolution of the
    screen and/or the text size, as they should be able to do.

    Not everyone uses your resolution and not everyone sees as well as you
    do. Text on a page should be able to be enlarged by using the browsers
    font-size option. If your CSS and layout can't handle that, change it.
    Note that when using CSS, using absolute font sizes messes up people's
    ability to change the font without using a custom style sheet (which
    Mozilla and Netscape users can do).

    --
    --
    ~kaeli~
    "No matter what happens, somebody will find a way to take
    it too seriously."



    Comment

    • ofer h

      #3
      Re: cliping a string without loops

      well, thanx for the quick reply.
      i understand what you mean by changing the resolution of the screen so
      the length of the string will change. that is true, but this is a
      thing that i can find out through the script. About changing the
      font-size - it doesnt apply to my problem because i am working on this
      project that the user will use it in class (or any other studying
      room), with a font size that i set before and is not changable by the
      user. the user will use IE 5+ with a resolution of usualy 600x800, and
      like i said before, that could be checked through the script.
      Is there any way to calculate the length in pixels, given the fact
      that i know the resolution and the font size (although font-size aplly
      only to the height of the font), or any other way to clip the string
      in a certain length. I dont realy care where the string will be
      clipped becuase i know the length of my div, so i know that for
      example i have to clip the string at 140px.
      thanx
      Ofer Harari

      Comment

      • Evertjan.

        #4
        Re: cliping a string without loops

        ofer h wrote on 06 jan 2004 in comp.lang.javas cript:
        [color=blue]
        > Is there any way to calculate the length in pixels, given the fact
        >[/color]

        Try this, under IE6 tested,
        the invisible span should not be horizontally confined:

        <span id="measure" style="visibili ty:hidden;font-size:10pt;">
        x</span>
        <script>
        str="Measure the length of this string"
        m=document.getE lementById("mea sure")
        m.innerText=str
        t1="The length of '"+str+"' is "
        t2=" pixels/fontsize point"
        alert(t1+m.offs etWidth/10+t2)

        str="And what about this string?"
        m.innerText=str
        t1="The length of '"+str+"' is "
        alert(t1+m.offs etWidth/10+t2)
        </script>



        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        Working...