Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So we have a /page.html and /folder/file.bla. We want to load that file contents as text string into some var using jQuery and call some function when we are done loading. How to do such thing?

like image 491
Rella Avatar asked Oct 26 '11 23:10

Rella


2 Answers

Get the file using $.AJAX :

$.ajax({
    type: 'GET',
    url: '/mypage.html',
    success: function (file_html) {
        // success
        alert('success : ' + file_html);
    }
});
like image 171
David Bélanger Avatar answered Oct 19 '22 17:10

David Bélanger


$.get('/folder/file.bla', function(data) {
  var fileContents = data;
});

The function you pass as the second argument to the get() function will be run once the data has been loaded from the external URL.

like image 40
Clive Avatar answered Oct 19 '22 17:10

Clive