获取外部文件的内容并定义为变量

Get contents of external file and define as variable

本文关键字:定义 变量 外部 文件 获取      更新时间:2023-09-26

是否可以将变量定义为外部文本文件的内容?

例子:

function getfile() {
        var textfromfile = (contents of http://example.com/plain.txt);
}

Plain.txt是一行,不是特别长。

因为我使用这个chrome扩展,我不允许使用php -有一个简单的方法来做到这一点在Javascript?

您可以使用jQuery进行ajax调用,并将响应分配给变量

  $.ajax({
    url: "plain.txt",
    cache: false
  })
  .done(function( html ) {
    useIt(html);
  });
  //Useful to use variable after callback
  function useIt(variable) {
    alert(variable);
  }