2016-11-08 50 views
0

我正在使用桌面通知。我使用这个代码来显示它,和它的做工精细:将文本文件的内容转换为javascript变量

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

+0

哪里是文本文件是从哪里来的? –

+0

如果你想加载一个文本文件,你可以使用Ajax。但是在使用Ajax之前,您需要设置一个Web服务器。 – undefined

+0

文本文件来自服务器.. notificion.txt – Ryewell

回答

1

适应从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); 
    }; 
} 
+0

谢谢..其工作! – Ryewell

0

例使用JQuery $.get()

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