<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>前端防護 &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/%E5%89%8D%E7%AB%AF%E9%98%B2%E8%AD%B7/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Tue, 30 Dec 2025 22:20:29 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://piglife.tw/wp-content/uploads/2017/10/cropped-logo-1-32x32.png</url>
	<title>前端防護 &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>JavaScript 實作禁止右鍵選單的簡易方法</title>
		<link>https://piglife.tw/technical-notes/javascript-disable-right-click/</link>
					<comments>https://piglife.tw/technical-notes/javascript-disable-right-click/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Tue, 30 Dec 2025 22:20:29 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[事件監聽]]></category>
		<category><![CDATA[前端防護]]></category>
		<category><![CDATA[右鍵禁用]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/javascript-disable-right-click/</guid>

					<description><![CDATA[介紹如何使用 JavaScript 監聽 contextmenu 事件並阻止預設右鍵選單，提供簡單有...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在某些網頁應用中，開發者可能希望防止使用者透過滑鼠右鍵開啟選單，以避免內容被輕易複製或操作。這段程式碼提供了一個簡單且不影響其他滑鼠功能的解決方案，適合有基礎 JavaScript 知識且想快速實作右鍵禁用的工程師或自學者。</p>
<h2 class="wp-block-heading">事件監聽與右鍵選單阻擋</h2>
<p>核心概念是監聽瀏覽器的 <code>contextmenu</code> 事件，該事件會在使用者嘗試開啟右鍵選單時觸發。透過呼叫 <code>event.preventDefault()</code>，可以阻止預設行為，也就是禁止顯示右鍵選單。</p>
<h3 class="wp-block-heading">重要程式碼片段</h3>
<pre><code class="lang-javascript language-javascript javascript">document.addEventListener(&quot;contextmenu&quot;, function (e) {
    e.preventDefault();
});</code></pre>
<p>這段程式碼綁定全域的 <code>contextmenu</code> 事件監聽器，當事件發生時直接阻止預設動作。</p>
<h2 class="wp-block-heading">為什麼用立即執行函式包裹？</h2>
<p>使用立即執行函式 (IIFE) 可以避免全域變數污染，確保程式碼在嚴格模式下運行 (<code>&quot;use strict&quot;</code>)，增加程式的穩定性與安全性。</p>
<h2 class="wp-block-heading">實務應用與注意事項</h2>
<ul>
<li>此方法只禁止右鍵選單，不會影響左鍵點擊、文字選取或快捷鍵操作，對使用者體驗影響較小。</li>
<li>但此防護並非絕對安全，仍可被瀏覽器開發者工具或其他方式繞過，適合用於降低一般使用者誤操作。</li>
<li>若需更嚴格的內容保護，建議搭配其他前端或後端策略。</li>
</ul>
<h2 class="wp-block-heading">延伸優化方向</h2>
<ul>
<li>可針對特定元素綁定 <code>contextmenu</code> 事件，避免全域禁用造成使用不便。</li>
<li>結合提示訊息告知使用者右鍵功能已被禁用，提高使用者理解。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-javascript language-javascript javascript">(function () {
    &quot;use strict&quot;;

    // 只鎖右鍵選單（不影響左鍵、選字、快捷鍵）
    document.addEventListener(&quot;contextmenu&quot;, function (e) {
        e.preventDefault();
    });
})();</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/javascript-disable-right-click/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
