0

So I aim to get some text from "clownJokes.txt" which is an external .txt and put that text into a variable inside my .js file.

Currently I know that $.get() from JQUERY calls Ajax and retrieves the file you want from the server. But I am searching for a way to do that locally, is there any way to do that?

$(document).ready(function(){
    $.get('clownJokes.txt', function(data){
        doSomethingWithData(data);
    }, 'text')
})

This is how it would look if I was to retrieve it from the server.

Thanks in advance!

David Bros
  • 78
  • 3
  • 9

2 Answers2

0

Try this link How do I load the contents of a text file into a javascript variable?

First answer is without jQuery, second answer is with jQuery.. your way might not be working because of the path.. press f12 and check console/network to see if you are actually able to fetch the file. if it is giving 404 then your problem is with the path to the file you want to extract

Hope this helps

Wbe
  • 23
  • 5
0

Loading locally is a huge security risk, making it not possible. I believe there's no way to do that in jQuery because of that risk. Generally, what you have above is a feasible way to do it if the TXT file was hosted on a web server somewhere.

HTML

<div class="result">
</div>

jQuery

$.get("PATH OF TXT FILE", function(data){
    $(".result").html(data);
  alert("Load was performed.");
});
ermalsh
  • 191
  • 1
  • 16