Today I realized that it will be better to have line numbers shown with the parts of code I publish into this blog.
I tried with google but didn't find any "easy" way to do it.
So I figured out my way which I am going to describe here!
First of all I wanted to show line numbers for the code but I also wanted the visitors to be able to copy the code without the line numbers.
As I'm using the <pre> tag to show my code I wrote a JavaScript function which will get the pre tags and change their innerHtml.
Here is the function:
I tried with google but didn't find any "easy" way to do it.
So I figured out my way which I am going to describe here!
First of all I wanted to show line numbers for the code but I also wanted the visitors to be able to copy the code without the line numbers.
As I'm using the <pre> tag to show my code I wrote a JavaScript function which will get the pre tags and change their innerHtml.
Here is the function:
| 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. | function showLineNumbers() { /************************************ * Written by Andreas Papadopoulos * * http://akomaenablog.blogspot.com * * akoma1blog@yahoo.com * ************************************/ var isIE = navigator.appName.indexOf('Microsoft') != -1; var preElems = document.getElementsByTagName('pre'); if (preElems.length == 0) return; for (var i = 0; i < preElems.length; i++) { var pre = preElems[i]; var oldContent = pre.innerHTML; oldContent = oldContent.replace(/ /g," "); var strs = oldContent.split("<br>"); if (isIE) { strs = oldContent.split("<BR>"); } oldContent = oldContent.substring(4); //remove the 1st <br> var newContent = "<table><tr>"; newContent = "<td bgcolor='#d4d0c8'>"; for(var j=1; j < strs.length - 1; j++) { newContent += j+".<br>"; } newContent += "</td><td> </td><td>"; newContent += oldContent; newContent += "</td></tr></table>"; pre.innerHTML = newContent; } } |
