JavaScript 與前端開發中有許多常見的小技巧,涵蓋表單控制、窗口操作、DOM 查詢、樣式限制與頁面效果。以下整理常用做法,方便查閱與套用。
oncontextmenu="return false"。onselectstart="return false"。onpaste="return false"。oncopy="return false" 與 oncut="return false"。style="ime-mode:disabled",但兼容性依瀏覽器而異。/^[0-9]+$/.test(value)。/[^\x00-\xff]/.test(value)。scrollHeight 並設定 height。top.location !== self.location。document.referrer。window.opener.location.reload()。window.resizeTo(width, height)。onblur="this.focus()"。<meta http-equiv="refresh" content="5;url=...">。Page-Enter 或 Page-Exit meta。overflow-x: hidden 或 overflow-y: hidden。background-attachment: fixed。document.documentElement.clientWidth 與 clientHeight。onfocus="this.blur()"。window.onerror,但正式環境仍應避免吞掉錯誤。date.getTime() - days * 86400000。<meta name="ROBOTS" content="index,follow">。<link rel="shortcut icon" href="favicon.ico">。<link rel="Bookmark" href="favicon.ico">。這些技巧多數屬於傳統前端操作,部分寫法依賴舊版瀏覽器。在現代專案中,建議優先使用標準 API、語意化 HTML 與可維護的事件模型。