document.write(""):輸出內容到文件(會覆寫整個頁面,除錯用而已)。//,多行 /* ... */。document → html → (head, body)。window → (navigator, screen, history, location, document)。String、Number、Boolean、Null、Object、Function、Undefined。parseInt()、parseFloat()(建議帶上 radix:parseInt(s, 10))。"" + 變數 或 String(變數)。length。字串串接用 + 或 +=。==、!=、>、>=、<、<=(相等判斷請優先用 ===)。var(現代的 let / const)。if (condition) {} else {}。for ([初始式]; [條件]; [更新式]) { }、while。break;跳過本次迭代:continue。function name([param], ...) { 敘述 }。\n。const;定義變數:var / let。Array、Boolean、Date、Error、Function、Math、Number、Object、RegExp、String,以及各類錯誤型別(EvalError、RangeError、ReferenceError、SyntaxError、TypeError、URIError)。document.getElementById('id');多個 form 時可用 document.forms[0]、document.forms[1]。document.getElementById('id').name / .value。document.forms.length。document.createElement()、document.createTextNode()。document.close()。contentEditable 設定元素是否可編輯,isContentEditable 讀取狀態。disabled 設定禁用狀態,isDisabled 讀取。element.tabIndex = 1。document.all.tags("div").length。清空表單中所有文字欄位:
var form = window.document.forms[0];
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].type === "text") {
form.elements[i].value = "";
}
}
核取方塊與單選按鈕:兩者都用 checked 屬性判斷是否被選中(回傳布林值)。單選按鈕組的名稱必須相同,組的長度用 document.forms[0].groupName.length。
下拉列表:document.forms[0].selectName.options[n].value,或用 selectedIndex 取得目前選項的索引。
window.open()、window.close();視窗自身:self。window.status = "文字";預設狀態列:window.defaultStatus。window.alert("文字");確認框:window.confirm();輸入框:window.prompt("訊息", "預設值")。window.location.href = "URL" 或 window.navigate()。location.reload()、parent.location.reload()、opener.location.reload()、top.location.reload()。history.back()、history.forward()、history.go(n)。window.print();捲動:window.scroll(x, y)、window.scrollBy()。window.focus();失焦:blur();全選:select()。setInterval(expr, ms) 與 setTimeout()。external.AddFavorite("url", "標題")。window.onerror = doNothing; 或指定處理函式。parent(父)、top(最頂層)、opener(開啟本視窗者,支援多層 opener.opener)、this(當前所屬物件)。frames[n].ObjFuncVarName、frames["frameName"].obj、frameName.obj。<a href="javascript:fn()">。<noscript></noscript>;用 <!-- ... //--> 包住舊式腳本。<script type="text/javascript" src="aaa.js"></script>。onload()、卸載 onunload()、關閉前 onbeforeunload。document.body.noWrap = true;。location 的屬性組成:
protocol // "http:" hostname // "www.example.com" port // "80" host // "www.example.com:80" pathname // "/a/a.html" hash // "#anchor",會跳到對應錨點 href // 完整的 URL
var myString = new String("...")(建議直接用字面量)。toUpperCase()、toLowerCase()。indexOf("sub") 回傳首次出現的索引(從 0 起算,找不到回傳 -1);lastIndexOf() 回傳最後一次。charAt(9);取字元編碼:charCodeAt(9)。substring(2, 6)、substr(start, length)、slice(start, end)。concat(string2) 或 +=。match(regExp)、replace(regExp, newStr)。split(分隔符) 回傳陣列。isNaN(變數) 判斷是否為非數字。常用數學函式:Math.PI(圓周率)、Math.SQRT2、Math.max(a, b)、Math.pow(a, 10)、Math.round()(四捨五入)、Math.floor(Math.random() * (n + 1))(隨機整數)。
建立日期物件:
var today = new Date();
取值方法:getTime()(毫秒數)、getFullYear()(四位年份)、getMonth()(月份,從 0 起算)、getDate()(日)、getDay()(星期幾,0 為週日)、getHours()、getMinutes()、getSeconds()。
設值方法:對應的 setTime()、setFullYear()、setMonth()、setDate()、setHours()、setMinutes()、setSeconds()。注意月份與星期皆從 0 開始計,getYear() 回傳的是兩位數年份,已不建議使用。
以上是日常最常翻閱的一批 API。實務上有兩點提醒:一是 document.write、document.all、window.navigate、showModalDialog 這類 API 都已老舊或僅 IE 支援,新專案請改用 DOM 標準方法;二是把這份清單存成文件或筆記,寫 code 時遇到卡住再回頭查,比死背有效得多。