How to load a file as a text string into some var using jQuery?

So we have /page.html and /folder/file.bla . We want to load the contents of this file into a text string in some var using jQuery and call some function when we are done loading. How to do it?

+6
source share
3 answers

Get the file using $ .AJAX:

 $.ajax({ type: 'GET', url: '/mypage.html', success: function (file_html) { // success alert('success : ' + file_html); } }); 
+8
source
 $.get('/folder/file.bla', function(data) { var fileContents = data; }); 

The function that you pass as the second argument to the get() function will be run after loading data from an external URL.

+7
source

What type of file? If its plain text, this should not be a problem :)

  $.get('/folder/file.bla', function(data) { var filetxt = data; // FINISHED GETTING FILE, INSERT CODE }); 
+3
source

Source: https://habr.com/ru/post/900168/


All Articles