早期 IE 專屬的 JavaScript 技巧集錦,包含右鍵禁用、防複製、圖標設定、框架控制、窗口操作等。**現代瀏覽器(Edge/Chrome/Firefox)均不支援**,僅供維護舊系統參考。
<body oncontextmenu="return false" onselectstart="return false" onpaste="return false" oncopy="return false" oncut="return false">
<link rel="Shortcut Icon" href="favicon.ico"> <!-- 地址列圖標 -->
<link rel="Bookmark" href="favicon.ico"> <!-- 收藏夾圖標 -->
<input style="ime-mode:disabled">
// 強制在框架中
if (window == top) top.location.href = "frames.htm";
// 防止被 iframe 嵌入
if (top.location != self.location) top.location = self.location;
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
<meta http-equiv="expires" content="0">
function getOffset(el) {
var top = 0, left = 0;
do {
top += el.offsetTop;
left += el.offsetLeft;
} while (el = el.offsetParent);
return { top: top, left: left };
}
function moveCursorToEnd(input) {
var range = input.createTextRange();
range.moveStart('character', input.value.length);
range.collapse(true);
range.select();
}
<input onfocus="moveCursorToEnd(this)">
// ASP
request.serverVariables("HTTP_REFERER")
// JS
document.referrer
<object id="minBtn" classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Minimize">
</object>
<button onclick="minBtn.Click()">最小化</button>
function isAllDigits(str) { return !/\D/.test(str); }
var w = document.body.clientWidth;
var h = document.body.clientHeight;
if (/[^\x00-\xff]/.test(str)) alert('含有中文');
<textarea rows="1" onpropertychange="this.style.posHeight=this.scrollHeight"></textarea>
function addDays(dateStr, days) {
var d = new Date(dateStr);
d.setDate(d.getDate() - days);
return d.getFullYear() + '年' + (d.getMonth()+1) + '月' + d.getDate() + '日';
}
| 舊功能 | 現代標準做法 |
|---|---|
| 禁用右鍵/複製 | CSS user-select: none、pointer-events: none(僅 UI 層面) |
| 框架控制 | CSP frame-ancestors、X-Frame-Options |
| 不被快取 | HTTP Header Cache-Control: no-store |
| 元素位置 | element.getBoundingClientRect() |
| 光標控制 | selectionStart/setSelectionRange() |
| 視窗大小 | window.innerWidth/innerHeight |
| 中文檢測 | /[\u4e00-\u9fff]/.test(str) |
| Textarea 自適應 | CSS resize: vertical 或 JS scrollHeight |