2016-11-08 4 views
0

Ich arbeite mit einer Desktop-Benachrichtigung. Ich verwende diesen Code, es zu zeigen, und seine adaequat:Holen Sie den Inhalt einer Textdatei in eine 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); 
} 

Aber wie kann ich holen Daten aus einer Textdatei in options.body?

+0

Wo wird die Textdatei kommen? –

+0

Wenn Sie eine Textdatei laden möchten, können Sie Ajax verwenden. Sie müssen jedoch vor der Verwendung von Ajax einen Webserver einrichten. – undefined

+0

Textdatei kommt vom Server .. ntificion.txt – Ryewell

Antwort

1

Anpassung des Codes von this answer, das fertige Ergebnis sollte wie folgt aussehen:

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

danke .. es funktioniert! – Ryewell

0

Beispiel JQuery mit $.get()

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