本文的範例使用了 IE 專有的 window.createPopup(),這是個在 2000 年代初期相當常見的做法,但早已被標準淘汰。以下保留原始實作作為參考,並在後面說明現代該怎麼做,以及為什麼「攔掉右鍵」通常不是好主意。
<head runat="server">
<title>自訂右鍵選單</title>
<script type="text/javascript">
var oPopup = window.createPopup();
function PopMenu(id) {
var oPopBody = oPopup.document.body;
oPopBody.style.backgroundColor = "buttonface";
oPopBody.style.border = "solid black 1px";
var TableBegin = "<table style=\"border:0;width:100%;font-size:12px;\" cellpadding=\"0\" cellspacing=\"0\">";
var Tr1 = "<tr><td valign=\"middle\" style=\"height:20px;\""
+ "onmousemove=\"this.bgColor='highlight';this.style.color='highlighttext'\""
+ "onmouseout=\"this.bgColor='';this.style.color=''\""
+ "onclick=\"parent.window.open('/news/view.asp?id=" + id + "');\">查看新聞</td></tr>";
var TrBr1 = "<tr><td style=\"background-color:ThreeDShadow;height:1px;\"></td></tr>";
var Tr2 = "<tr><td valign=\"middle\" style=\"height:20px;cursor:hand;\""
+ "onmousemove=\"this.bgColor='highlight';this.style.color='highlighttext'\""
+ "onmouseout=\"this.bgColor='';this.style.color=''\""
+ "onclick=\"parent.document.frames['main'].location='divdiv.aspx?id=" + id + ";'\">查看新聞</td></tr>";
var TableEnd = "</table>";
oPopBody.innerHTML = TableBegin + Tr1 + TrBr1 + Tr2 + TableEnd;
oPopup.show(event.x, event.y, 90, 41, document.body);
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href='javascript:void(0);' oncontextmenu='PopMenu(1);return false;'>
<img alt="" src='1.gif' style="border:0;width:150px;height:110px;cursor:hand" />
</a>
<iframe name="main" frameborder="0" width="100%" scrolling="auto" height="100%"></iframe>
</div>
</form>
</body>
關鍵是 oncontextmenu 回傳 false,藉此取消瀏覽器預設的右鍵選單,再把自訂的彈出層顯示在滑鼠位置。
<style>
#context-menu {
position: fixed;
min-width: 140px;
background: #fff;
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: 0 2px 8px rgba(0,0,0,.15);
padding: 4px 0;
font-size: 14px;
z-index: 1000;
}
#context-menu[hidden] { display: none; }
#context-menu button {
display: block;
width: 100%;
border: 0;
background: none;
text-align: left;
padding: 6px 16px;
cursor: pointer;
}
#context-menu button:hover,
#context-menu button:focus-visible { background: #eee; }
</style>
<div id="ctx-target" style="padding:40px;background:#f5f5f5;">在此處按右鍵</div>
<nav id="context-menu" role="menu" hidden>
<button type="button" role="menuitem">查看詳情</button>
<button type="button" role="menuitem">複製連結</button>
<button type="button" role="menuitem">刪除</button>
</nav>
<script>
(function () {
var target = document.getElementById('ctx-target');
var menu = document.getElementById('context-menu');
target.addEventListener('contextmenu', function (e) {
e.preventDefault();
menu.hidden = false;
// 先顯示才能量到尺寸,再據此避免超出視窗
var mw = menu.offsetWidth, mh = menu.offsetHeight;
menu.style.left = Math.min(e.clientX, window.innerWidth - mw) + 'px';
menu.style.top = Math.min(e.clientY, window.innerHeight - mh) + 'px';
// 讓鍵盤使用者也能操作
var first = menu.querySelector('button');
if (first) first.focus();
});
// 點其他地方或按 Escape 就關掉
document.addEventListener('click', function (e) {
if (!menu.contains(e.target)) menu.hidden = true;
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape') { menu.hidden = true; }
});
window.addEventListener('resize', function () { menu.hidden = true; });
window.addEventListener('scroll', function () { menu.hidden = true; });
})();
</script>
全域攔右鍵通常得不償失。使用者依賴右鍵選單做的事很多:檢查元素、複製圖片網址、拼字檢查、開新分頁、存成書籤。把這些一併拿掉,會讓網站變得難用,也擋不住任何有決心的人。除非有明確的功能需求(例如圖片版權保護的輔助手段、或像 Google Docs 那樣右鍵選單本身就是核心功能),否則只在特定元素上攔截,並保留「檢視原始碼」這類 Developers 應有的能力。
鍵盤操作不能少。右鍵選單對只用鍵盤的使用者是無法存取的。範例中在開啟後把焦點移到第一個項目,並支援 Escape 關閉,是最低限度。若要更完整,還應該支援上下鍵在項目間移動、Enter 執行,並用 role="menu" / role="menuitem" 標示語意(如範例所示)。
注意邊界。彈出位置要避開視窗右下角,範例用 Math.min 夾住座標就是這個用意。
點擊外部要關閉。除了點擊,捲動與視窗尺寸變動時也應該關閉,否則選單會「浮」在錯誤的位置。
最後補充一個安全提醒:原範例把 id 直接字串拼接進 onclick 的 JavaScript 裡。若 id 來自使用者輸入,這是一個 XSS 弱點。現代做法應改用 addEventListener 並透過 dataset 傳值,避免組出可執行的程式碼字串。