將文本文件的內容放入javascript變量中

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


I'm working with a desktop notification. I'm using this code to show it, and its working fine:

我正在使用桌面通知。我正在使用此代碼來顯示它,並且它的工作正常:

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

But how can I fetch data from a text file into options.body?

但是,如何將文本文件中的數據提取到options.body中?

2 个解决方案

#1


2  

Adapting the code from this answer, the finished result should look like this:

調整此答案的代碼,完成的結果應如下所示:

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

#2


0  

Example using JQuery $.get()

使用JQuery $ .get()的示例

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/11/08/72f1ea224ba7c2f410dda16aa3a8e715.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com