繁体   English   中英

将文本文件的内容获取到javascript变量中

[英]Get the contents of a text file into a javascript variable

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

// 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呢?

根据此答案改编代码,最终结果应如下所示:

// 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);
  };
}

使用JQuery $.get()示例

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM