How to read a .txt file and convert to string in javascript? [duplicate]
This question already has an answer here:
Upload File as String to JavaScript Variable
2 answers
I am trying to use javascript to read a .txt file, but I cannot seem to find any methods online. The purpose of the code is to scan a text file for a certain string. In my code the user purposely uploads the file and it is stored in a variable, but i cannot find a way to actually read the text in the file and convert into a string. Any help would be appreciated, thanks.
document.getElementById("answer").addEventListener(search);
document.getElementById("actualAns").addEventListener(search);
function myFunction() {
var targetAns = document.getElementById("answer").value; //the user inputs a .txt file
var actualAns = document.getElementById("actualAns").value; //the user enters the string they want to search
var n = actualAns.search(targetAns); // this part does not work, it should search the text file but it only searches the name
if(n > -1) {
document.getElementById("result").innerHTML = "Correct";
} else {
document.getElementById("result").innerHTML = "Incorrect";
}
}
javascript jquery html text
marked as duplicate by str, Mohammad, charlietfl
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 18:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Upload File as String to JavaScript Variable
2 answers
I am trying to use javascript to read a .txt file, but I cannot seem to find any methods online. The purpose of the code is to scan a text file for a certain string. In my code the user purposely uploads the file and it is stored in a variable, but i cannot find a way to actually read the text in the file and convert into a string. Any help would be appreciated, thanks.
document.getElementById("answer").addEventListener(search);
document.getElementById("actualAns").addEventListener(search);
function myFunction() {
var targetAns = document.getElementById("answer").value; //the user inputs a .txt file
var actualAns = document.getElementById("actualAns").value; //the user enters the string they want to search
var n = actualAns.search(targetAns); // this part does not work, it should search the text file but it only searches the name
if(n > -1) {
document.getElementById("result").innerHTML = "Correct";
} else {
document.getElementById("result").innerHTML = "Incorrect";
}
}
javascript jquery html text
marked as duplicate by str, Mohammad, charlietfl
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 18:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Upload File as String to JavaScript Variable
2 answers
I am trying to use javascript to read a .txt file, but I cannot seem to find any methods online. The purpose of the code is to scan a text file for a certain string. In my code the user purposely uploads the file and it is stored in a variable, but i cannot find a way to actually read the text in the file and convert into a string. Any help would be appreciated, thanks.
document.getElementById("answer").addEventListener(search);
document.getElementById("actualAns").addEventListener(search);
function myFunction() {
var targetAns = document.getElementById("answer").value; //the user inputs a .txt file
var actualAns = document.getElementById("actualAns").value; //the user enters the string they want to search
var n = actualAns.search(targetAns); // this part does not work, it should search the text file but it only searches the name
if(n > -1) {
document.getElementById("result").innerHTML = "Correct";
} else {
document.getElementById("result").innerHTML = "Incorrect";
}
}
javascript jquery html text
This question already has an answer here:
Upload File as String to JavaScript Variable
2 answers
I am trying to use javascript to read a .txt file, but I cannot seem to find any methods online. The purpose of the code is to scan a text file for a certain string. In my code the user purposely uploads the file and it is stored in a variable, but i cannot find a way to actually read the text in the file and convert into a string. Any help would be appreciated, thanks.
document.getElementById("answer").addEventListener(search);
document.getElementById("actualAns").addEventListener(search);
function myFunction() {
var targetAns = document.getElementById("answer").value; //the user inputs a .txt file
var actualAns = document.getElementById("actualAns").value; //the user enters the string they want to search
var n = actualAns.search(targetAns); // this part does not work, it should search the text file but it only searches the name
if(n > -1) {
document.getElementById("result").innerHTML = "Correct";
} else {
document.getElementById("result").innerHTML = "Incorrect";
}
}
This question already has an answer here:
Upload File as String to JavaScript Variable
2 answers
javascript jquery html text
javascript jquery html text
edited Nov 19 '18 at 20:05


dustinos3
4551616
4551616
asked Nov 19 '18 at 18:36


sparposparpo
1918
1918
marked as duplicate by str, Mohammad, charlietfl
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 18:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by str, Mohammad, charlietfl
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 '18 at 18:40
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can read the file like this:
function readFile() {
jQuery.get("file.txt", function(textString)) {
//do what you want with the textString
});
}
and in the textString you will have the string of text present in your file.
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 19 '18 at 23:32
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can read the file like this:
function readFile() {
jQuery.get("file.txt", function(textString)) {
//do what you want with the textString
});
}
and in the textString you will have the string of text present in your file.
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 19 '18 at 23:32
add a comment |
You can read the file like this:
function readFile() {
jQuery.get("file.txt", function(textString)) {
//do what you want with the textString
});
}
and in the textString you will have the string of text present in your file.
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 19 '18 at 23:32
add a comment |
You can read the file like this:
function readFile() {
jQuery.get("file.txt", function(textString)) {
//do what you want with the textString
});
}
and in the textString you will have the string of text present in your file.
You can read the file like this:
function readFile() {
jQuery.get("file.txt", function(textString)) {
//do what you want with the textString
});
}
and in the textString you will have the string of text present in your file.
edited Nov 20 '18 at 16:34
answered Nov 19 '18 at 18:47


D. DianaD. Diana
314
314
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 19 '18 at 23:32
add a comment |
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 19 '18 at 23:32
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 19 '18 at 23:32
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
– Nick
Nov 19 '18 at 23:32
add a comment |