将文本文件的内容放入javascript变量中
我正在处理桌面通知。我正在使用此代码来显示它,并且它工作正常:将文本文件的内容放入javascript变量中,javascript,jquery,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 = ne
// 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()的示例
文本文件来自哪里?如果您想加载文本文件,可以使用Ajax。但在使用Ajax之前,您需要设置一个web服务器。文本文件来自服务器。。notification.txt可能重复的(只需使用XHR.responseText
而不是切片)您能将放入我的代码中吗。。对不起,我刚刚接触javascript。。
if (permission === "granted") {
$.get("notificion.text", function(data) {
var notification = new Notification("Hi there", {
icon: "icon.jpg",
dir: "ltr",
body: data
});
});
}