HTML5 verwandelt den Browser von einem bloßen Dokumentbetrachter in ein Multitalent!
![]() |
![]() |
||
|---|---|---|---|
| Grafiken generieren | ✔ | ✖ | ✔ |
| Sound erzeugen | ✔ | ✖ | ✔ |
| Videos abspielen | ✔ | ✖ | ✔ |
| Echtzeit-Networking | ✔ | ✖ | ✔ |
| Multithreading | ✔ | ✖ | ✔ |
| Funktioniert offline | ✔ | ✖ | ✔ |
// Neuen Listenpunkt einfügen
var addItem = function(item){
if(item) $('#list').append($('<li>' + item + '</li>'));
}
// Vorhandene Einträge anzeigen
var loadItems = function(){
$('#list').html('');
$.get('read.php', null, function(data){
data.split("\n").map(function(item){
addItem(item);
});
});
}
// Neuen Eintrag abspeichern
$('#new').submit(function(evt){
evt.preventDefault();
if($('#date').val() && $('#date').val()){
var item = $('#date').val() + ': ' + $('#todo').val();
$.get('save.php', { q: item }, addItem.bind(null, item));
}
});
// Beim laden der Seite Einträge vom Server holen
$(window).load(function(){
loadItems();
});
return navigator.onLine; // Einfache Status-Abfrage
window.addEventListener('online', function(){ // Online-Event
alert('Ab jetzt online!');
}, false);
window.addEventListener('offline', function(){ // Offline-Event
alert('Ab jetzt offline!');
}, false);
// Online-Status-Anzeige ändern
var changeStatusDisplay = function(){
var status = (navigator.onLine) ? 'Online' : 'Offline';
$('#status').attr('class', status.toLowerCase()).html(status);
}
// On- und Offline-Events abfangen
$(window).bind({
'online': changeStatusDisplay,
'offline': changeStatusDisplay
});
// Beim laden der Seite Einträge vom Server holen
$(window).load(function(){
loadItems();
changeStatusDisplay(); // Status-Anzeige aktualisieren
});
// Vorhandene Einträge anzeigen
var loadItems = function(){
$('#list').html('');
if(navigator.onLine){
$.get('read.php', null, function(data){
data.split("\n").map(function(item){
addItem(item);
});
});
}
else {
alert('Offline-Modus: Kann Einträge nicht laden');
}
}
// Neuen Eintrag abspeichern
$('#new').submit(function(evt){
evt.preventDefault();
if($('#date').val() && $('#date').val()){
var item = $('#date').val() + ': ' + $('#todo').val();
if(navigator.onLine){
$.get('save.php', { q: item }, addItem.bind(null, item));
}
else {
alert('Offline-Modus: Speichern nicht möglich!');
}
}
});
// Besser Online-Status-Detektor
navigator.onlineStatus = (function(){
var pingSuccess = function(){
if(!navigator.onlineStatus){
navigator.onlineStatus = true;
$(window).trigger('online');
}
}
var pingFailure = function(){
if(navigator.onlineStatus){
navigator.onlineStatus = false;
$(window).trigger('offline');
}
}
var ping = function(timeout){
var start = Date.now();
$.ajax('ping.php', {
'complete': function(xhr, txt){
var time = Date.now() - start, success = (txt == 'success' && time < timeout);
if(success) pingSuccess();
else pingFailure();
}
});
}
setInterval(ping.bind(null, 1000), 2000);
return navigator.onLine;
})();
CACHE MANIFEST foo.html images/bild.png scripts/jquery.js
CACHE MANIFEST about.html styles.css jquery.js scripts.js # Die index.html brauchen wir nicht aufzulisten # Dort binden wir das Manifest ein
<html manifest="pfad/zum/manifest.manifest">
// Cache-Status-Anzeige ändern
var changeCacheStatusDisplay = function(status){
var titles = {
'checking' : 'Caching läuft...',
'noupdate' : 'Cache bereit',
'downloading' : 'Caching läuft...',
'progress' : 'Caching läuft...',
'cached' : 'Cache bereit',
'updateready' : 'Cache bereit',
'obsolete' : 'Cache-Fehler',
'error' : 'Cache-Fehler'
};
$('#cacheStatus').attr('class', status).html(titles[status]);
}
// Cache-Events abfangen
'checking|noupdate|downloading|progress|cached|updateready|obsolete|error'.split('|')
.map(function(evt){
$(applicationCache).bind(evt, changeCacheStatusDisplay.bind(null, evt));
});
if(window.localStorage){ // Wenn speichern möglich ist...
localStorage.setItem('foo', 42); // ... speichern wir etwas...
return localStorage.getItem('foo'); // ... und geben es wieder aus!
}
if($.jStorage.storageAvailable()){ // Wenn speichern möglich ist...
$.jStorage.set('foo', 42); // ... speichern wir etwas...
return $.jStorage.get('foo'); // ... und geben es wieder aus!
}
Funktioniert in:
✔ Internet Explorer ab 6.0 (!)
✔ Firefox ab 2.0 (!)
✔ Safari ab 4.0
✔ Opera ab 10.50
✔ iPhone & Android
// Neuen Eintrag abspeichern
$('#new').submit(function(evt){
evt.preventDefault();
if($('#date').val() && $('#date').val()){
var item = $('#date').val() + ': ' + $('#todo').val();
if(navigator.onLine){
$.get('save.php', { q: item }, addItem.bind(null, item));
}
else {
alert('Offline-Modus: Speichern nicht möglich!');
}
}
});
// Neuen Eintrag abspeichern $('#new').submit(function(evt){ evt.preventDefault(); if($('#date').val() && $('#date').val()){ var item = $('#date').val() + ': ' + $('#todo').val(); if(navigator.onLine){ $.get('save.php', { q: item }, addItem.bind(null, item)); }else { if($.jStorage.storageAvailable()){ var lastIndex = $.jStorage.index().length, slot = 'slot' + lastIndex; $.jStorage.set(slot, item); addItem(item); } }} });
// Vorhandene Einträge anzeigen
var loadItems = function(){
$('#list').html('');
if(navigator.onLine){
$.get('read.php', null, function(data){
data.split("\n").map(function(item){
addItem(item);
});
});
}
else {
alert('Offline-Modus: Kann Einträge nicht laden');
}
}
// Vorhandene Einträge anzeigen var loadItems = function(){ $('#list').html(''); if(navigator.onLine){ $.get('read.php', null, function(data){ data.split("\n").map(function(item){ addItem(item); }); }); }else { if($.jStorage.storageAvailable()){ var keys = $.jStorage.index(); keys.map(function(key){ var item = $.jStorage.get(key); addItem(item); }); } }}
// Synchronisierungsfunktion
var sync = function(){
if($.jStorage.storageAvailable()){
var keys = $.jStorage.index();
if(keys.length > 0 && navigator.onLine){
console.log('Synchronisiere ' + keys.length + ' Einträge');
var to_sync = keys.length, synced = 0;
keys.map(function(key){
var item = $.jStorage.get(key);
$.get('save.php', { q: item }, function(){
$.jStorage.deleteKey(key);
++synced;
if(synced == to_sync){
loadItems();
}
});
});
}
}
}
// On- und Offline-Events abfangen
$(window).bind({
'online': function(){
changeStatusDisplay();
sync();
},
'offline': changeStatusDisplay
});
// Beim laden der Seite Einträge vom Server holen
$(window).load(function(){
loadItems();
sync();
changeStatusDisplay(); // Status-Anzeige aktualisieren
});
Fragen?
peter@peterkroener.de