我在玩與http://www.w3.org/TR/offline-webapps/navigator.onLine
發現不完整的榜樣,但是,我心疼地看到它的評論一樣:
"renders the note somewhere", and
"report error", and
"// …"
因此,將有人請幫我寫一個有效例?下面是我到目前爲止有:
<!DOCTYPE HTML>
<html manifest="cache-manifest">
<head>
<script>
var db = openDatabase("notes", "", "The Example Notes App!", 1048576);
function renderNote(row) {
// renders the note somewhere
}
function reportError(source, message) {
// report error
}
function renderNotes() {
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS Notes(title TEXT, body TEXT)',
[]);
tx.executeSql(‘SELECT * FROM Notes’, [], function(tx, rs) {
for(var i = 0; i < rs.rows.length; i++) {
renderNote(rs.rows[i]);
}
});
});
}
function insertNote(title, text) {
db.transaction(function(tx) {
tx.executeSql('INSERT INTO Notes VALUES(?, ?)', [ title, text ],
function(tx, rs) {
// …
},
function(tx, error) {
reportError('sql', error.message);
});
});
}
</script>
<style>
label {
display:block;
}
</style>
</head>
<body>
<form>
<label for="mytitle">Title:</label>
<input name="mytitle">
<label for="mytext">Text:</label>
<textarea name="mytext"></textarea>
<!-- There is no submit button because I want to save the info on every keystroke -->
</form>
</body>
</html>
我也知道,我在那裏的某個地方將這一:
if (navigator.onLine) {
// Send data using XMLHttpRequest
} else {
// Queue data locally to send later
}
但我不知道是什麼連我將配合這一點。
我會建議不要在每次擊鍵時保存到數據庫。使用setTimeout在用戶暫停鍵入後觸發保存* n *秒。否則,網絡延遲會導致輸入一個噩夢。我自己,我會把保存到每個輸入的onblur處理程序中。 – Robusto 2010-03-01 18:38:06
感謝Robusto的評論。 – 2010-03-01 22:54:09