以下整理 HTML5 各項能力的簡短範例,可作為開發時的速查參考。部分 API 帶有廠商私有前綴,實際使用時建議搭配 Chrome 或 Safari 等 WebKit 系瀏覽器測試,並加上能力偵測。
// localStorage 用於長期保存;sessionStorage 則限於單一分頁
textarea.addEventListener('keyup', function () {
window.localStorage['value'] = area.value;
window.localStorage['timestamp'] = (new Date()).getTime();
}, false);
textarea.value = window.localStorage['value'];
var db = window.openDatabase("Database Name", "Database Version");
db.transaction(function (tx) {
tx.executeSql("SELECT * FROM test", [], successCallback, errorCallback);
});
<html manifest="cache-manifest">
window.applicationCache.addEventListener('checking', updateCacheStatus, false);
對應的 cache-manifest 內容:
CACHE MANIFEST # version 1 CACHE: /html5/src/refresh.png /html5/src/logic.js /html5/src/style.css /html5/src/background.png
// main.js
var worker = new Worker('extra_work.js');
worker.onmessage = function (event) {
alert(event.data);
};
// extra_work.js
// 進行運算,完成後回傳訊息
postMessage(some_data);
var socket = new WebSocket(location);
socket.onopen = function (event) {
socket.postMessage("Hello, WebSocket");
};
socket.onmessage = function (event) {
alert(event.data);
};
socket.onclose = function (event) {
alert("closed");
};
if (window.webkitNotifications.checkPermission() == 0) {
window.webkitNotifications
.createNotification(tweet.picture, tweet.title, tweet.text)
.show();
} else {
window.webkitNotifications.requestPermission();
}
document.addEventListener('dragstart', function (event) {
event.dataTransfer.setData('text', 'Customized text');
event.dataTransfer.effectAllowed = 'copy';
}, false);
<body>
<header>
<hgroup>
<h1>Page title</h1>
<h2>Page subtitle</h2>
</hgroup>
</header>
<nav>
<ul>Navigation...</ul>
</nav>
<section>
<article>
<header><h1>Title</h1></header>
<section>Content...</section>
</article>
</section>
<aside>Top links...</aside>
<footer>Copyright © 2009.</footer>
</body>
<link rel="alternate"> <link rel="icon"> <link rel="nofollow"> <link rel="prefetch"> <a rel="archives"> <a rel="external"> <a rel="license"> <a rel="noreferrer"> <a rel="pingback"> <a rel="sidebar"> <a rel="tag">
<input type="range" min="0" max="50" value="0"> <input autofocus type="search"> <input type="text" placeholder="Search inside"> <menu> <progress> <input type="color"> <input type="number"> <input type="email"> <input type="tel"> <input type="time"> <input type="date"> <input type="month"> <input type="week"> <input type="datetime">
<audio src="sound.mp3" controls></audio>
document.getElementById("audio").muted = false;
<video src="movie.mp4" autoplay controls></video>
document.getElementById("video").play();
<canvas id="canvas" width="838" height="220"></canvas>
<script>
var canvasContext = document.getElementById("canvas").getContext("2d");
canvasContext.fillRect(250, 25, 150, 100);
canvasContext.beginPath();
canvasContext.arc(450, 110, 100, Math.PI * 1/2, Math.PI * 3/2);
canvasContext.lineWidth = 15;
canvasContext.lineCap = 'round';
canvasContext.strokeStyle = 'rgba(255, 127, 0, 0.5)';
canvasContext.stroke();
</script>
@font-face { font-family: 'Junction'; src: url(Junction02.otf); }
@font-face { font-family: 'LeagueGothic'; src: url(LeagueGothic.otf); }
header { font-family: 'LeagueGothic'; }
.row:nth-child(even) { background: #dde; }
.row:nth-child(odd) { background: white; }
:not(.box) { color: #00c; }
:not(span) { display: block; }
h2:first-child { ... }
div.text > div { ... }
h2 + header { ... }
input[type="text"] { background: #eee; }
-webkit-column-count: 6; -webkit-column-rule: 1px solid #bbb; -webkit-column-gap: 2em;
-webkit-text-fill-color: black; -webkit-text-stroke-color: red; -webkit-text-stroke-width: 0.00px;
color: rgba(255, 0, 0, 0.41); background: rgba(0, 0, 255, 0.19); color: hsla(184, 75%, 33%, 0.15);
border-radius: 7px; background: -webkit-gradient(linear, left top, left bottom, from(#00abeb), to(white), color-stop(0.5, white), color-stop(0.5, #66cc00)); background: -webkit-gradient(radial, 430 50, 0, 430 50, 590, from(red), to(#000)); text-shadow: rgba(0, 0, 0, 0.5) 0 -1px -1px;
-webkit-box-reflect: below 10px
-webkit-gradient(linear, left top, left bottom,
from(transparent), to(rgba(255, 255, 255, 0.78)));
#box {
-webkit-transition: margin-left 1s ease-in-out;
}
@-webkit-keyframes pulse {
from { opacity: 0.0; font-size: 100%; }
to { opacity: 1.0; font-size: 200%; }
}
div {
-webkit-animation-name: pulse;
-webkit-animation-duration: 2s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-direction: alternate;
}
上面這些 -webkit- 前綴的屬性在制訂當時多屬實驗性質,規格與語法後續有所調整,部分寫法(如舊版的 -webkit-gradient)已被標準的 linear-gradient、radial-gradient 取代;Web SQL Database 也已被廢棄,改用 IndexedDB。這份清單適合理解當時的能力邊界,實際專案請以現行標準為準。