Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature currently requires accessing the site using the built-in Safari browser.
Wie befüllst du sie denn jetzt. also beim ersten Aufruf?Nur wie kann ich denn aus einer js Funktion genau die Teile im html aktualisieren die meine Werte aus dem Array anzeigen?
<link rel="stylesheet" href="Menu.css">
<script>
var items = {};
function getJSON(url) {
var resp ;
var xmlHttp ;
resp = '' ;
xmlHttp = new XMLHttpRequest();
if(xmlHttp != null)
{
xmlHttp.open( "GET", url, false );
xmlHttp.send( null );
resp = xmlHttp.responseText;
}
return resp ;
}
function initi(){
var gjson;
gjson = getJSON('http://192.168.178.52:8080/rest/items?recursive=false') ;
obj=JSON.parse(gjson);
obj.forEach(function(entry) {
var name = entry.name;
items[name] = {value:entry.state};
console.log(name, items[name].value);
});
}
initi();
console.log("Werte sind initialisiert");
// Event-Stream
var eventSource = new EventSource("http://192.168.178.52:8080/rest/events");
eventSource.addEventListener('message', function(eventPayload){
var event = JSON.parse(eventPayload.data);
if (event.type === "ItemStateEvent"){
var name = event.topic.split('/')[2];
items[name] = JSON.parse(event.payload);
console.log(name, items[name].value);
}
console.log("events werden empfangen");
console.log(name, items[name].value);
});
</script>
<div class="flex-1">
<ul class="lamps__list">
<li class="lamps__item lamps__total">
<h2 class="lamps__count" id="id1"><script type="text/javascript">document.write(items['FEG_HZ_Soll'].value)</script></h2>
<p class="lamps__description">Total Lamps</p>
</li>
<li class="lamps__item lamps__active">
<h2 class="lamps__count" id="id2"><script type="text/javascript">document.write(items['FEG_HZ_Ist'].value)</script></h2>
<p class="lamps__description">Active Lamps</p>
</li>
</ul>
</div>
http://www.w3.org/html/wg/drafts/html/CR/webappapis.html#dynamic-markup-insertion schrieb:This method has very idiosyncratic behavior. In some cases, this method can affect the state of the HTML parser while the parser is running, resulting in a DOM that does not correspond to the source of the document (e.g. if the string written is the string "<plaintext>" or "<!--"). In other cases, the call can clear the current page first, as if document.open( ) had been called. In yet more cases, the method is simply ignored, or throws an exception. To make matters worse, the exact behavior of this method can in some cases be dependent on network latency, which can lead to failures that are very hard to debug. For all these reasons, use of this method is strongly discouraged.
{
mal in der selben Zeile und mal in der nächsten Zeile.'
und mal mit dopelten Anführungszeichen "
.;
am Zeilenende var resp ;
solltest du entfernen: var resp;
type
-Attribut <script type="text/javascript">
und mal ohne <script>
.document.write()
wurdest du ja schon hingewiesen.var
initialisierst, kannst du auch gleich einen Wert zuweisen, bzw. auch in einem Block (Komma separiert) definieren bzw. zuweisen:
var resp;
var xmlHttp;
resp = '';
xmlHttp = new XMLHttpRequest();
var resp = '';
var xmlHttp = new XMLHttpRequest();
var resp = '',
xmlHttp = new XMLHttpRequest();
body
-Tags) geladen ist. Also entweder mit window.onload = initi;
ober direkt vor dem schließenden body
-Tag mittels script
-Tag:
...
<script>
initi();
</script>
</body>
name
auch sparen:
function initi(){
var gjson = getJSON('http://192.168.178.52:8080/rest/items?recursive=false'),
eventSource = new EventSource('http://192.168.178.52:8080/rest/events');
obj = JSON.parse(gjson);
obj.forEach(function (entry) {
items[entry.name] = {value:entry.state};
console.log(entry.name, items[entry.name].value);
});
console.log('Werte sind initialisiert');
updateValues();
// Event-Stream
eventSource.addEventListener('message', function (eventPayload) {
var event = JSON.parse(eventPayload.data);
if (event.type === 'ItemStateEvent') {
var name = event.topic.split('/')[2];
items[name] = JSON.parse(event.payload);
console.log(name, items[name].value);
}
console.log('events werden empfangen');
console.log(name, items[name].value);
updateValues();
});
}
items
(FEG_HZ_Soll, FEG_HZ_Ist...) anscheinend sowieso "einmalig" sind, würde ich diese als id
in deinem HTML verwenden:
<div class="flex-1">
<ul class="lamps__list">
<li class="lamps__item lamps__total">
<h2 class="lamps__count" id="FEG_HZ_Soll"></h2>
<p class="lamps__description">Total Lamps</p>
</li>
<li class="lamps__item lamps__active">
<h2 class="lamps__count" id="FEG_HZ_Ist"></h2>
<p class="lamps__description">Active Lamps</p>
</li>
</ul>
</div>
function updateValues() {
for (var name in items) {
if (items.hasOwnProperty(name)) {
document.getElementById(name).textContent = items[name].value;
}
}
console.log('Werte wurden aktualisiert');
}