admin管理员组

文章数量:1405288

On www.example/page_a/ is it possible to run a javascript code that searches for the text "hello" inside the file located at www.example/page_b/my_file.txt ?

Is this possible with javascript/jquery/ any other javascript framework?

On www.example./page_a/ is it possible to run a javascript code that searches for the text "hello" inside the file located at www.example./page_b/my_file.txt ?

Is this possible with javascript/jquery/ any other javascript framework?

Share Improve this question edited Oct 12, 2016 at 10:46 shubham715 3,3021 gold badge19 silver badges27 bronze badges asked Oct 12, 2016 at 10:39 MalasorteMalasorte 1,1837 gold badges23 silver badges47 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Yes, it is possible and it is simplest with jquery.

You need to "get" contents of the text file like this:

$.get("www.example./page_b/my_file.txt", function(contents){
   //contents variable now contains the contents of the textfile as string

   //check if file contains the word Hello
   var hasString = contents.includes("Hello");

   //outputs true if contained, else false
   console.log(hasString);
})

Try using jquery $.get()

$.get("text file path",function(textString) {
    // handle the returned string ..
},"text/plain")

I am using indexOf() to search a string in a text file using JavaScript. Here is the code

$.get("www.example./page_b/my_file.txt", function(contents){
   var str = "Hello";
   if(contents.indexOf(str) != -1){
       console.log(str + " found");
   }
})

本文标签: jquerySearch inside txt file using javascriptStack Overflow