2

I've been trying to find the answer to this on my own but have come up short.

Using JS/Jquery in html file, I need to:

  1. When a link is pressed it loads a file located on the server.
  2. Search the file for word x
  3. If that word doesn't appear or appears only once, do nothing. If it appears 2 or more times open y url.

For now I can't even properly load a file as a variable.

This is what I have tried so far:

var myvar = $.get("localhost/readme.txt").responseText; 
document.write(myvar);
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • I'm still trying to load the file. I've tried var myvar = $.get("http://localhost/readme.txt").responseText; document.write(myvar); and something similar but using the "load" function. – user2166038 Mar 13 '13 at 16:41
  • I could be wrong here, but you probably need an http:// before your localhost. – Paddy Mar 14 '13 at 09:19

2 Answers2

3

No longer applies to question following edit...


You can't access a client's file system via Javascript - see

Local file access with javascript

for more details.

There is some limited support coming with the HTML 5 implementation, but this might not suit for your needs:

http://www.html5rocks.com/en/tutorials/file/filesystem/

Community
  • 1
  • 1
Paddy
  • 33,309
  • 15
  • 79
  • 114
  • by local I meant a file on the server, not on the clients computer, sorry for the mistake, i've edited the post above. – user2166038 Mar 13 '13 at 16:44
  • From the documentation at your link "Load data from the *server* and place the returned HTML into the matched element.". Loads data from the remote server, not the client. – Paddy Mar 13 '13 at 16:44
  • I know. That's what I need my script to do. – user2166038 Mar 13 '13 at 17:16
0

You were close, you just weren't accessing the responseText properly. Also, don't use document.write() to output test data, use the console.

$.get("localhost/readme.txt").done(function(filecontents) {
    console.log(filecontents);
    // now you can search through filecontents as plain text.
    if (filecontents.match(/myword/ig).length >= 2) {
        alert("DO STUFF!");
    }
})
Kevin B
  • 94,570
  • 16
  • 163
  • 180