www.webdeveloper.com
Results 1 to 6 of 6

Thread: open,search and copy contents of a text document

  1. #1
    Join Date
    Jan 2009
    Posts
    3

    open,search and copy contents of a text document

    Hi could anyone provide me javascript code for the task that im having ....i find it a bit difficult being a newbie.....the task is .....i have a .rtf file ..a text file.....i need to open it first to my text area.......then search for some keywords in it.....as soon as the keyword is highlighted ....there should be a button named copy.....which will copy the searched contents in a new window......kindly help...many thanks....

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    There are a couple of different ways to do what your describing. One way that probably won't work is to just use Javascript for the whole thing. You will run into security blockage problems from most browsers when you try to read the data from the file. To work around this you can use a server-side script such as php or asp to load the data from the text file. Then you can use Javascript to read through the processed content to match for your string and make the highlights you need.

    Alternatively you can use a small java applet to provide all of those abilities without needing multiple scripts and the java applet would run locally as well as on a server. Another similar option would be to build a flash swf that could also provide the features you are looking for. Actionscript is nearly identical to javascript as far as syntax if you aren't looking to learn anything all that new.

  3. #3
    Join Date
    Jan 2009
    Posts
    3
    thanks for the reply.....but whatever it may be....can u provide me the code.....if u can....so that i can run it the platform that u say....

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    Since you haven't specified what the specific environment is you are developing for I will give you links to appropriate topics from each of my suggestions above.


    If you want to use Java:
    http://java.sun.com/docs/books/tutorial/essential/io/

    If you want to use Flash:
    http://www.flashadvisor.com/tutorial...o_flash-3.html

    If you want to use PHP:
    http://www.tizag.com/phpT/fileread.php
    The accompanying JavaScript info you would need:
    http://www.jsmadeeasy.com/javascript.../innerHtml.htm

  5. #5
    Join Date
    Jan 2009
    Posts
    3
    hi...i have got the code to open the text file.....its as follows...

    <html>
    <head><title>snook.ca load document</title>
    <script language="JavaScript">

    function loadworddoc(){
    var doc = new ActiveXObject("Scripting.FileSystemObject");
    var file = doc.OpenTextFile(document.getElementById("filename").value, 1, true);
    var txt = file.ReadAll()
    document.all.tbContentElement.DOM.body.innerHTML = txt;
    file.Close()
    }

    </script>
    </head>
    <body>
    <p><input type=button onClick="loadworddoc();" value="Load">
    <p><input type=file id="filename">
    <p><textarea name=myarea cols=50 rows=5>nothing here yet</textarea>
    <object ID="tbContentElement" CLASS="tbContentElement"
    CLASSID="clsid:2D360201-FFF5-11D1-8D03-00A0C959BC0A" VIEWASTEXT
    width="450" height="300">
    <param name=Scrollbars value=true></object>
    </body>
    </html>


    now i need to search something in this....for example a word...how can i do it....my algorithm for search is as follows

    ALGORITHM FOR SEARCHING

    1. A SEARCH TEXT BOX AND A BUTTON WITH A SEARCH ATTRIBUTE IS CREATED.
    2. AN ARRAY IS CREATED TO CHECK A WORD INSIDE THE TEXT DOCUMENT THAT IS TYPED IN THE SEARCH BAR.
    3. THE ARRAY WILL RETURN TRUE ONLY WHEN THE WORD MATCHES THE WORD TYPED IN THE SEARCH BAR.
    4. FOR EVERY OTHER INSTANCE THE ARRAY WILL RETURN FALSE OR A STATEMENT "SORRY WORD NOT FOUND".
    5. WHEN THE WORD MATCHES THAT OF THE WORD IN THE SEARCH BAR, IT IS THEN HIGHLIGHTED.

    kindly help me with the code for searching the word...many thanks....

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    There is a major problem with using ActiveX controls in a webpage. Your site will only work in IE.

    If you know that and know that the intended audience will only be using IE then your solution will be fine.


    ALGORITHM FOR SEARCHING

    1. A SEARCH TEXT BOX AND A BUTTON WITH A SEARCH ATTRIBUTE IS CREATED.

    document.write(html stuff here);

    2. AN ARRAY IS CREATED TO CHECK A WORD INSIDE THE TEXT DOCUMENT THAT IS TYPED IN THE SEARCH BAR.

    var tempString = form.textarea.value;
    var arrWords = split(tempString, " ");

    3. THE ARRAY WILL RETURN TRUE ONLY WHEN THE WORD MATCHES THE WORD TYPED IN THE SEARCH BAR.

    for(var i =0; i < arrWords;i++){
    .....if(searchWord == arrWords[i]){
    ..........do something
    .....}
    }

    4. FOR EVERY OTHER INSTANCE THE ARRAY WILL RETURN FALSE OR A STATEMENT "SORRY WORD NOT FOUND".

    else {
    ....do something else
    }

    5. WHEN THE WORD MATCHES THAT OF THE WORD IN THE SEARCH BAR, IT IS THEN HIGHLIGHTED.

    if you are displaying text in a textarea in html this will not be possible.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles