irwan

COOL TRUNCATE with JAVASCRIPT !!

Oct 10th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5.  
  6. <head>
  7. <title>Hello!</title>
  8. </head>
  9.  
  10. <body>
  11.  
  12. <p id="truncateMe">New features were added and bugs were fixed since alpha1.
  13. Please help us to identify bugs by testing new features and looking for unintended
  14. backward compatability breaks, so we can fix the problems and fully document intended
  15. changes before PHP 5.4.0 is released.</p>
  16.  
  17. <script type="text/javascript">
  18.  
  19. var len = 100;
  20. var p = document.getElementById('truncateMe');
  21. if (p) {
  22.  
  23. var trunc = p.innerHTML;
  24. if (trunc.length > len) {
  25.  
  26. /* Truncate the content of the P, then go back to the end of the
  27. previous word to ensure that we don't truncate in the middle of
  28. a word */
  29. trunc = trunc.substring(0, len);
  30. trunc = trunc.replace(/\w+$/, '');
  31.  
  32. /* Add an ellipses to the end and make it a link that expands
  33. the paragraph back to its original size */
  34. trunc += '<a href="#" ' +
  35. 'onclick="this.parentNode.innerHTML=' +
  36. 'unescape(\''+escape(p.innerHTML)+'\');return false;">' +
  37. 'read more<\/a>';
  38. p.innerHTML = trunc;
  39. }
  40. }
  41.  
  42. </script>
  43.  
  44. </body>
  45.  
  46. </html>
  47.  
Advertisement
Add Comment
Please, Sign In to add comment