tags:

views:

147

answers:

3

Hey
I assume my title basically summed it all up.

I have a <h2> title, and I want it to have a max character property, be it CSS or javascript, so that whenever this maximum limit is passed, the title's end is replaced by ... (three dots).

Thank you very much in advance.
An example can be viewed here: http://themeforest.net/forums/thread/now-accepting-bargain-submissions/23205 (see the title)

P.S. Any idea why I had to create a new user? the login-feature through google doesn't recognize my previous user :(

A: 

Pure javascript style

var maxLen = 30;
var titles = document.getElementsByTagName("h2");
for(i in titles){
  if(titles[i].innerHTML.length > maxLen){
    titles[i].innerHTML = titles[i].innerHTML.substr(0, maxLen)+"...";
  }
}

Doing this by CSS you could set width for h2 with overflow: hidden

Anpher
Hey, thanks a bunch. I actually did it with CSS, using oferflow:hidden, but it just cut out in the middle of a letter, and that didn't look good. Trying your script now.
cr0z3r
Well, I tried it, but it doesn't work :S. I assume I am doing something wrong; I've added the code in the head of my HTML within <script type="text/javacsript"></script> and also tried tweaking the maxLen var, with 10 or so, but it doesn't work :(
cr0z3r
You have to execute this on dom ready (when all h2 elements are loaded). `<script type="text/javacsript"> window.onload = function(){ ... } </script>`
Anpher
Excuse my ignorance, but I tried it again. I replaced those ... in your last example with your above code. I tried putting it in the head and the body, neither worked.Have a look: http://pastebin.com/y7D3kZqk
cr0z3r
change `text/javacsript` to `text/javascript` :D
Anpher
A: 

You can do it with CSS by specifying a fixed width for H2 elements, or width JavaScript:

function shorten() {
    maxlength = 20;
    h2 = document.getElementsByTagName('h2');
    for (i = 0; i < h2.length; i++) {
        title = h2[i].innerText;
        if (title.length > maxlength) {
            h2[i].innerHTML = title.substr(0, maxlength) + "&hellip;";
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", shorten);
Midas
I must be doing something wrong, as the code below as well as this one aren't working. Any hints?
cr0z3r
I edited my code. Try putting this in your head section. As Anpher said, the page has to load all the H2 elements before executing the code, so attach it to the `onload` event. Else it will be executed before the whole page is loaded and wont find any H2 elements to shorten.
Midas
Sorry, still nothing. I created a function called init() with your code and load it in <body onload="init()">, yet it doesn't work. You can see it here: http://pastebin.com/9iREKXrZPlease be patient with me, I am new to javascript functions, and loading beforehand. Thanks
cr0z3r
You don't need to put it in `<body onload>`. Just put my code in a `script` tag in your `head` section.
Midas
A: 

If using a hellip is not necessary, you can consider this pure-css solution:

h2 {
    max-width: 300px;
    white-space: nowrap;
    overflow: hidden;
}

h2:before {
    content: " "; /* unicode space Alt+255 */
    display: block;
    background: url(shadow.png) top right repeat-y;
    height: 100%;
    width: 20px;
    float: right;
    position: relative;
}

shadow.png is small horizontal gradient from transparency to your background color. Unfortunately, there is some problem in IE.

tambourine
Woah, that's stunning. I tried it, and it works, though when I use the alt+255 blank space, it shows a diagonal square with a '?'.
cr0z3r
Strange. Does your page have utf-8 encoding? Anyway, you can use other technique, like `content: "."; padding-left: 1000px;`Other way is to add at the end of header tag some container `<h2>My TItile<span class="shadow"></span></h2>` where .shadow class is the same with :before. This should solve IE issues.
tambourine
Hey, I sadly realised it would work, but it doesn't apply to what I need. Please have a look at my title (it must allow 2-lined titles, so if I use CSS and put a max-width, it'd only work on 1-lined h2 titles. >> http://i45.tinypic.com/2q03889.jpg
cr0z3r