-3

Possible Duplicate:
smart way to shorten long strings with javascript

I have

<h1>"Very big" page title </h1>

page title and I need to check if character count is bigger than 30, than replace last characters with ...

Can I somehow split text in two parts and replace second part?

Community
  • 1
  • 1
Lex
  • 15
  • 1
  • 4

2 Answers2

2

You have to check title's length with jQuery. If it is greater than 30, split it to the specified length and add ... in the end.

Regex is useless here.

$('h1').each(function(){
  var text = $(this).text();
  if ( text.length > 30 ) {
    $(this).text( text.substring(0, 30) + '...' );
  }
});
hsz
  • 148,279
  • 62
  • 259
  • 315
2

Code may be like this:

var text = $('h1').text();
if(text.length > 30)
   $('h1').text(text.sustring(0,30) + "...")

and no need for regexp.

Viktor S.
  • 12,736
  • 1
  • 27
  • 52