Display of line numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amulyab
    New Member
    • Dec 2007
    • 14

    Display of line numbers

    Hi all,
    I want to display the line numbers for the text which resides inside the text field.How can i do this?Please help me.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    please post what you have so far ... so that we might have an example to work with ...

    kind regards

    Comment

    • amulyab
      New Member
      • Dec 2007
      • 14

      #3
      [HTML]<HTML>
      <HEAD>
      <SCRIPT>
      function readFile (fileName) {
      alert("aaa");
      if (document.layer s && navigator.javaE nabled()) {

      netscape.securi ty.PrivilegeMan ager.enablePriv ilege('Universa lFileRead');
      var bfr = new java.io.Buffere dReader(new
      java.io.FileRea der(fileName));
      var line;
      var content = '';
      while ((line = bfr.readLine()) != null)
      content += line + java.lang.Syste m.getProperty(' line.separator' );
      return content;
      }
      else if (document.all) {
      alert("bbb");
      var fso = new ActiveXObject(' Scripting.FileS ystemObject');
      var fs = fso.OpenTextFil e(fileName);
      var result = fs.ReadAll();
      return result;
      }
      }
      </SCRIPT>



      </HEAD>
      <BODY>
      <FORM NAME="formName" >
      <INPUT TYPE="file" NAME="fileName" >
      <INPUT TYPE="button" VALUE="show"
      ONCLICK="this.f orm.fileContent .value =
      readFile(this.f orm.fileName.va lue)"
      >
      <BR>
      <TEXTAREA NAME="fileConte nt" ROWS="20" COLS="80" WRAP="on"></TEXTAREA>
      </FORM>
      </BODY>
      </HTML>[/HTML]

      [HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">
      <html lang="en">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>selectin g a line in a textarea in Mozilla</title>
      <script type="text/javascript">
      function selectLine (textarea, text1,text2) {
      alert("aaa");
      var text = textarea.value;
      alert(text);
      lineNumber1 = Number(text1);
      alert(Number(te xt1));
      lineNumber2 = Number(text2);
      alert(Number(te xt2));
      var linePattern = /.*(\r\n|\r|\n)/g;
      alert(linePatte rn);
      linePattern.las tIndex = 0;
      var lineStart = 0;
      var lineEnd = 0;
      var lineMatch;
      var lineFound = 0;
      var found = false;
      while ((lineMatch = linePattern.exe c(text))) {

      lineFound++;
      lineStart = lineMatch.index ;

      lineEnd = lineStart + lineMatch[0].length;
      alert(lineEnd);
      if (lineFound == lineNumber2 ) {
      found = true;
      break;
      }
      }
      if (found) {
      if (textarea.setSe lectionRange) {
      textarea.scroll Top = Math.floor(text area.scrollHeig ht /
      countLines(text ) * (lineNumber2 - 1));
      textarea.setSel ectionRange(lin eStart, lineEnd);
      }
      }
      }

      function countLines (text) {
      var linePattern = /.*(\r\n|\r|\n)/g;
      linePattern.las tIndex = 0;
      var lineCount = 0;
      var match, index, matchText;
      while ((match = linePattern.exe c(text))) {
      lineCount++;
      index = match.index;
      matchText = match[0];
      }
      if (index + matchText.lengt h < text.length) {
      lineCount++;
      }
      return lineCount;
      }
      </script>
      </head>
      <body>
      <form action="">
      <p>
      <label>
      example textarea:
      <br>
      <textarea name="textareaN ame" rows="10" cols="80">
      Line 1
      Line 2
      Line 3
      Line 4
      Line 5
      Line 6
      Line 7
      Line 8
      Line 9
      Line 10
      Line 11
      Line 12
      Line 13
      Line 14
      Line 15
      </textarea>
      </label>
      </p>
      <p>
      <label>line number:</label>
      <input type="text" name="text1" id="text1">
      <input type="text" name="text2" id="text2">
      <input type="button"
      value="select line"
      onclick="select Line(this.form. elements.textar eaName,this.for m.elements.text 1.value,this.fo rm.elements.tex t2.value);">
      </p>
      </form>
      </body>
      </html>
      [/HTML]
      1. In this code the 1st javascript reads the uploaded file and displays the contents in the text area,but this works only in IE,i want the same function to be worked in the Mozilla Firefox and also along with the contents it should display the line numbers.

      2. In my second javascript i need to change the code like,if i select the start and end line numbers which are entered in 2 text boxes, it should highlight the contents.
      Please please help me.Its very urgent. :-(
      Last edited by gits; Dec 21 '07, 08:03 AM. Reason: added code tags

      Comment

      Working...