最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to load a file as a text string into some var with jQuery? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Oct 29, 2011 at 2:34 Clive 37k8 gold badges89 silver badges113 bronze badges asked Oct 26, 2011 at 23:18 RellaRella 66.9k112 gold badges373 silver badges642 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

Get the file using $.AJAX :

$.ajax({
    type: 'GET',
    url: '/mypage.html',
    success: function (file_html) {
        // success
        alert('success : ' + file_html);
    }
});
$.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.

What type is the file? If its pure text, it shouldn't be a problem :)

   $.get('/folder/file.bla', function(data) {
    var filetxt = data;
    // FINISHED GETTING FILE, INSERT CODE
    });
发布评论

评论列表(0)

  1. 暂无评论