PDA

View Full Version : unable to read file content



needsomehelp
Nov 19th, 2014, 05:48 PM
for some reason I am unable to read the file contents, or it could be something else, I added in some extra code to see where the script got to and I only get to see '1' on the output.

Is there something else I have missed ?

I am wanting to ready each line one by one and replace a phrase with another, then output the new line.

I visit the 1.txt file in my browser and the file shows fine.



var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://mysite.com/convertJS/1.txt", true);
txtFile.onreadystatechange = function()
{
document.write("1<br>");
if (txtFile.readyState === 4) { // document is ready to parse.
document.write("2<br>");
if (txtFile.status === 200) { // file is found
document.write("3<br>");
allText = txtFile.responseText;
lines = txtFile.responseText.split("\n");

document.write(lines + "<br>");

// I shall be altering the text in each line, search and replace, so only need to be able to read one line at a time.

}
}
}
txtFile.send(null); // close file.

sorry that i used the quote method to post, but when i used [ code ] it corupted the text as the document . write lines?!

sunfighter
Nov 19th, 2014, 06:14 PM
txtFile.open("GEThhttp://mysite.com/convertJS/1.txt", true);
Should be:
txtFile.open("GET", "http://mysite.com/convertJS/1.txt", true);

DrDOS
Nov 19th, 2014, 06:19 PM
The 1 comes from this line, document.write("1<br>"); because when you use document.write it writes to the original page and wipes out everything on it, including any javascript or other code, and replaces it with that,. Obviously you can't use it. And what does an example of 1.txt look like? Javascript has trouble with anything but well formed javascript.

needsomehelp
Nov 19th, 2014, 07:37 PM
strange, when i checked the qoute above it changed...

here is another version that I now have...



var varName = "_0x7268";

var _0x7268 = ["cat", "dog", "car", "fish"];

function conv() {
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://mysite.com/convertJS/1.txt", true);
txtFile.onreadystatechange = function()
{
var newFileContent = "";
if (txtFile.readyState === 4) { // document is ready to parse.
if (txtFile.status === 200) { // file is found
allText = txtFile.responseText;
lines = txtFile.responseText.split("\n");
// while (lines.search(varName) != -1) {
// // string.indexOf(searchvalue,start)
// startChar = lines.indexOf(varName,0); // 0 is the first char.
// endNum = lines.indexOf("]",startChar);
// startOfNum = startChar + varName.length;
// lines = str.substring(startOfNum, endNum - 1); // 0 is the first char.
// }
newFileContent += lines;
}
}
}
txtFile.send(null); // close file.
write(newFileContent + "<br>");
}
conv();

rnd me
Nov 19th, 2014, 07:58 PM
use console.log instead of document.write for these sort of things.

needsomehelp
Nov 19th, 2014, 08:53 PM
I checked the web console in firefox and no javascript shows up at all.

Old Pedant
Nov 19th, 2014, 09:02 PM
In any case, *STOP* using document.write. Period. Learn to use DOM methods and properties.

needsomehelp
Nov 19th, 2014, 09:03 PM
I am now getting...

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ...
This can be fixed by moving the resource to the same domain or enabling CORS.

needsomehelp
Nov 19th, 2014, 09:11 PM
sorry i have decided to give up on this idea and look for another method as i have spent long enough and asked to much help on this one. thank you all the same.

Decided to convert it manually as I think I could have done it three times over today with the time spent on trying to do it with a script. lol

rnd me
Nov 19th, 2014, 09:36 PM
I am now getting...

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ...
This can be fixed by moving the resource to the same domain or enabling CORS.

you run the code from the devtools console while on the site to execute ajax on the resources, or use greaemonkey or tampermonkey or node.js to run it.

Old Pedant
Nov 19th, 2014, 09:46 PM
Or do this in PHP (or JSP or ASP or..) server-side code where the "cross origin" policy does not exist.