0

Currently I am just seeing if I can get anything from the text file to show up on my website so I can work with it. I've checked multiple questions on here, but I feel like my lack of knowledge with web development is showing here.

So my javascript function is this...

function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
    if(rawFile.readyState === 4)
    {
        if(rawFile.status === 200 || rawFile.status == 0)
        {
            var allText = rawFile.responseText;
          document.write(allText);
        }
    }
}
rawFile.send(null);
}

So ideally I would think that this would just write the text onto the page. So I call it within the page like this...(I'm not terribly creative just thought this should work).

<body onload="readTextFile('info.txt')">

What am I missing ?

  • Not sure if you can load *text* file directly as it would turn into security concerns for browsers. You can load in javascript files which could contain variables that you require. – Sagar Aug 07 '17 at 05:40
  • At which browser are you trying code at Question? – guest271314 Aug 07 '17 at 05:43
  • homework assignment. Requirement is to get username and password info from a text file, however I'm asking this in a way so I don't directly get the answer since I should be able to figure out what to do when I get this working with a split. –  Aug 07 '17 at 05:44
  • browser isn't specified in the requirements unfortunately, but maybe chrome? –  Aug 07 '17 at 05:44
  • At chrome or chromium you would need to launch the browser with `--allow-file-access-from-files` flags set for ability to access `file:` protocol – guest271314 Aug 07 '17 at 05:46
  • Still not seeing any of the text file text posted on the website. Possibility that my code is wrong? –  Aug 07 '17 at 06:00
  • Okay, update it opens the file with microsoft edge, but not chrome. Thanks –  Aug 07 '17 at 06:12
  • Okay so it loads a whole page with the text file, however I want to keep the login page loaded and just want to pull the data from the text file and set it as variables. –  Aug 07 '17 at 06:23

0 Answers0