将文本文件的内容放入javascript变量中

时间:2016-11-08 01:37:44

标签: javascript jquery

我正在处理桌面通知。我使用此代码来显示它,并且其工作正常:

// If the user is okay, let's create a notification
if (permission === "granted") {
  var options = {
    body: "This is the body of the notification",
    icon: "icon.jpg",
    dir : "ltr"
  };
  var notification = new Notification("Hi there",options);
}

但是如何将文本文件中的数据提取到options.body

2 个答案:

答案 0 :(得分:2)

调整this answer的代码,完成的结果应如下所示:

// If the user is okay, let's create a notification
if (permission === "granted") {
  var options = {
    icon: "icon.jpg",
    dir : "ltr"
  };
  var XHR = new XMLHttpRequest();
  XHR.open("GET", "notificion.txt", true);
  XHR.send();
  XHR.onload = function (){
    options.body = XHR.responseText;
    var notification = new Notification("Hi there",options);
  };
}

答案 1 :(得分:0)

使用JQuery $.get()

的示例
if (permission === "granted") {
  $.get("notificion.text", function(data) {
    var notification = new Notification("Hi there", {
      icon: "icon.jpg",
      dir: "ltr",
      body: data
    });
  });
}