Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Possible to read from a file with JavaScript?

hippychic43hippychic43 Posts: 19Member
[b][red]This message was edited by hippychic43 at 2005-2-2 22:23:11[/red][/b][hr]
Hi,

I saw an earlier post asking if it was possible to read and write files with JavaScript and an answer stated that Active X was needed for writing, but what about if I only want to read a file? Is there anyway I can read a text file with Javascript to be able to analyse the contents of the text file?

I only want to be able to examine the files in my own website, nothing on the user's computer or anything.

Thanks
Cristy


«1

Comments

  • the walrusthe walrus Posts: 249Member
    umm, you certainly can't read files on the user's computer (think of what a security risk that would be if a website could just read your files and email them off to whoever it wanted), but yes there are ways to read files from the server. the way you would go about doing that, and the complexity of the script, kind of depends on what kind of files you wanted to read and what you wanted to do with those files. there's not really a basic solution i could post that would work for general needs; could you give me more information about what you wanted to do?
  • hippychic43hippychic43 Posts: 19Member
    [b][red]This message was edited by hippychic43 at 2005-2-6 20:7:53[/red][/b][hr]
    Ok, the website I am creating isn't a website for the internet it is a website for a CD that is being used to present some content. I've created a Search Engine in Java Script that searches through a JS Array for keywords that the user searches for, and when it finds a match it brings up the details of the pages that are matches on another page. The problem is the JS Array only contains a few keywords that the pages contain. What I want to do is create an advanced search that actually traverses all of the HTML pages to see if a keyword that the user searches on actually exists in the whole documentation. So I only want to be able to open HTML files and read what's in them to see if it matches a keyword the user has entered as a search term.

    I've noticed the mention of Active X controls before, is it necessary to develop an Active X control in Java to accomplish this? Or is there one already available I just have to use it?

    Thanks


  • the walrusthe walrus Posts: 249Member
    here's a little example that might be able to do what you want. in this case it loads the text of the pages into a string, which could be used for processing, but the example just shows the value of the string in an alert box. there's probably a better way to do this, but this is what i came up with:

    [code]



    Click "Show Pages" to see page test1.html, test2.html, and test3.html in an alert box.



    [/code]

    to see this example you'll need to save this in a .html file and also make files test1.html, test2.html, and test3.html and save them in the same folder. when you click the button, you should be displayed the contents of test1.html, test2.html, and test3.html. let me know if you have any more questions or problems.
  • hippychic43hippychic43 Posts: 19Member
    thanks heaps!

    I didn't think it was possible with just Javascript, I think I should be able to adapt that perfectly to be able to do what I want.

    Thanks
    Cristy
  • hippychic43hippychic43 Posts: 19Member
    Hi,

    i've been testing out that code and such and have found that the document.all[0].innerText only works in IE, I need it to work across browsers, does anyone know of anyway to read a documents contents that exists in an iframe? or even the current frame? I've been looking through all of the methods and properties available and can't find anyway to return the text that is in the document, any help would be appreciated.

    Thanks
    Cristy
  • the walrusthe walrus Posts: 249Member
    [b][red]This message was edited by the walrus at 2005-2-7 17:25:55[/red][/b][hr]
    [code]



    Click "Show Pages" to see page test1.html, test2.html, and test3.html in an alert box.



    [/code]

    the changes that were needed for it to work in firefox were:

    1) give the iframe a name property so that firefox loads it correctly into the frames[] array

    2) change src="#" to src="about:blank".. not necessary, but i noticed in firefox that the # cause the page to repeat over and over in the iframe, which would just waste memory, so about:blank would be a better way to do that

    3) firefox doesn't seem to put the frames[] array in the document object, so get rid of the [b]document.frames['FrameX'][/b] part and just change it to [b]frames['FrameX'][/b].

    4) change document.all[0].inner[b]Text[/b] to document.all[0].inner[b]HTML[/b]. for some reason, it seems that firefox doesn't support innerText. unfortunately, using innerHTML means that you'll get all the tags along with the text, but for your purposes that probably wont really even matter. [blue]Note: if you only want to get the stuff inside the tag you can do [b]document.body.innerHTML[/b] instead.[/blue]

    these changes dont affect the script's ability to work in ie. i also tested in opera 7 and it worked fine with the changes.

    hope this helps,
    Nathan


  • hippychic43hippychic43 Posts: 19Member
    What do you use as a reference for JavaScript? Because the references I've been using don't list properties for document.body as in the innerHTML and innerText, it just lists the properties and methods for the first level of objects in the HTML DOM hierarchy, as in the document object, window, text etc.

    I would love to get my hands on a reference that went even more indepth.

    Thanks
  • the walrusthe walrus Posts: 249Member
    [b][red]This message was edited by the walrus at 2005-2-7 21:11:28[/red][/b][hr]
    : What do you use as a reference for JavaScript? Because the references I've been using don't list properties for document.body as in the innerHTML and innerText, it just lists the properties and methods for the first level of objects in the HTML DOM hierarchy, as in the document object, window, text etc.
    :
    : I would love to get my hands on a reference that went even more indepth.
    :
    : Thanks
    :

    well, honestly google is the best js reference anyone could ever ask for. usually you can kind of guess what the methods are going to be or type in a phrase that'll get you plenty of information. but to answer your question, msdn has a pretty good list of js objects, properties, and methods. http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/body.asp

    msdn isn't always the best at explaining things, but once you know the name of something you can google for it and find a lot more information, including how to get it to work in other browsers (msdn is obviously going to be specific to ie).
  • WeirdofreakWeirdofreak Posts: 439Member
    Personally, I use http://www.quirksmode.org for reference, but as the walrus said, Google is also very useful. I also wrote a script to recursively list the properties of an object, but just names, it isn't always easy to work out what you need, and not all browsers list everythig - IE, for instance, won't show methods.
    ^D
    $ shameless-plug
    http://line-ed.sourceforge.net

  • kallietkalliet Posts: 1Member
    Hello,

    I haven't been writing much javascript code so far and I have been looking for this code for a few days now.
    The example with the button works great, but I need to read that file when loading the page, so I can print a part of the content in the page.
    I can't seem to invoke this.

    If I remove the button and replace it by

    loadPage(Pages[0]);


    --> then I get the error that xf.document is null

    Suggestions?

    thx,
    kalliet
«1
Sign In or Register to comment.