Disabling the right-click menu on a web page takes one line of JavaScript attached to the document's contextmenu event, usually document.addEventListener("contextmenu", event => event.preventDefault()). But that handler only suppresses the browser's menu. The page source, cached files, DevTools, keyboard shortcuts, and saved copies remain reachable, and Firefox, Chrome, and screen-reader users handle the block differently. You'll get working code in the steps below, plus the reasons this belongs to a broader protection strategy rather than standing alone.
The rest of this guide walks through three implementation levels—inline HTML, a site-wide script, and a framework component—then covers what each one actually blocks, how to test it, where it fails (mobile long-press, iframes, keyboard menus), and which alternatives offer real protection when the goal is photo protection, quiz integrity, or a cleaner UI.
What the right-click handler does and does not stop
The browser fires a contextmenu event whenever a user triggers the context menu: a mouse right-click, a two-finger tap on macOS, a long-press on touchscreens, or the menu key on Windows keyboards. Calling preventDefault() on that event cancels the default popup. The page continues running normally.
That mechanism is why the trick is popular and why it is widely misunderstood. A handler blocks one UI affordance. It does not, by itself, prevent:
View Source (Ctrl+U on Windows/Linux, Cmd+Option+U on macOS Safari). DevTools (F12, Ctrl+Shift+I, Ctrl+Shift+J, or Cmd+Option+I). Save Page As (Ctrl+S / Cmd+S), which copies the rendered HTML and its assets. Print to PDF, screen capture, or opening a link in a new tab while the menu is disabled. Direct asset URLs typed into the address bar, which bypass the page entirely.
The handler also interacts badly with accessibility tools. Some screen readers and keyboard-only users open the browser context menu to reach "Open link in new tab" or spellcheck options, and a blanket block on right-click also silences that path for them. That's the accessibility cost you're accepting, and it's worth naming up front.
Prerequisites before adding any blocking code
You need edit access to the page's HTML or to a JavaScript file it loads, plus a way to reload the page and a browser to test in. No additional library is required for the basics.
Decide first what problem you're solving. If it's keeping casual visitors from copying images, a contextmenu handler raises friction but doesn't stop screenshots. If it's keeping quiz answers out of the DOM, disabling right-click is theater—the answers are in the page source regardless. If it's creating a kiosk-style interface or a full-screen presentation where accidental clicks derail the experience, the technique is reasonable. That distinction should decide whether you go further into the anti-copy measures described later.
For existing sites, note where your scripts load. A blocking script in must defer or listen on DOMContentLoaded, otherwise document may not be ready depending on placement.
Level 1: Block right-click on a single page with inline HTML
This is the fastest version and good for a one-off page. It must be the first thing a reader encounters after your tag or placed in with a listener that waits for the DOM.
The oncontextmenu="return false" attribute returns false from the handler, which is the earliest and simplest way to cancel the event. Some corporate content filters and browser extensions rewrite or strip inline handlers, which is one reason to keep the more portable scripted version handy.
The success check is trivial: put the cursor anywhere on the page, right-click, and confirm no menu appears. Confirm that ordinary clicks, text selection, and scrolling still work, since a malformed handler can accidentally swallow other events.
A limitation to know about: inline handlers don't survive HTML sanitizers. If your page content is produced through a CMS field, a Markdown renderer, or a templating engine that strips on* attributes, this method silently fails and you'll spend time wondering why. In that case, escalate to Level 2.
Level 2: Add a site-wide script that catches every trigger
Inline handlers work but don't cover the keyboard menu key, some touch long-presses, and pages where you forget to add the attribute. A proper event listener in an external script handles all pages that include it.
Include the script after the closing or with defer in :
defer guarantees the script runs after HTML parsing but before DOMContentLoaded fires, which is why the listener wrapper is safe. Replace the path with whatever serves your static files.
Test the result in Chrome, Firefox, and Safari. Right-click a paragraph, a link, and an image. Try the keyboard menu key. Then on a phone or with DevTools' device emulation, long-press an image (Chrome on Android) and use a two-finger tap on macOS. You should see no menu, and the page should still scroll and click normally. If one browser still shows the menu, the handler is likely being attached before the document exists; check that defer or DOMContentLoaded is in place.
An edge case: if some of your users legitimately need right-click for an in-page tool—say a canvas editor or a spellcheck extension—the global handler becomes hostile. Prefer scoping the block to a specific container:
This limits the effect to a single region and leaves the browser menu intact elsewhere.
Level 3: Framework implementations (React, Vue, plain modules)
Modern single-page applications usually mount components rather than attach listeners at document load. The React version is compact:
Setting draggable="false" on the image additionally stops the drag-to-desktop shortcut some browsers expose. The Vue equivalent uses @contextmenu.prevent on any element:
Angular uses (contextmenu)="onContextMenu($event)" and calls event.preventDefault() in the handler. All three approaches scope the block to the component, which is usually what you want: readers can still right-click your navigation or text elsewhere.
A subtle failure mode in React: if the element is inside a portal or rendered conditionally, the listener attaches and detaches cleanly, but a stale test can mislead you. Reload after every change and check that ordinary right-clicks elsewhere still produce the browser menu, which confirms you haven't accidentally blocked the whole page.
Stopping what right-click bails out of: copy events and selection
Right-click is one route to copying. Two others are worth covering alongside it because they share the same trade-off: cumbersome for you to maintain, confusing for users who expect them.
To discourage text selection and copying inside a protected area:
And CSS that turns off text selection can be applied directly:
These work together. A reader who can't select fingers the text can still type it out, still view source, and still open a saved HTML file in a text editor. None of this is DRM; it's a fence that signals intent to the honest visitor while a determined one steps around it. If the intent is to signal "please don't copy this" to polite users, this is the right level of effort.
Where the block breaks: mobile, iframes, and keyboard paths
Browser behavior in this area has shifted over time and differs across platforms, so test per target rather than trusting a blog post's promise.
iOS Safari and long-press. Long-pressing a link or image opens the native preview and share sheet. A contextmenu handler doesn't consistently stop that sheet, and documentation from MDN shows mobile Safari fires contextmenu inconsistently across versions. If mobile is a real audience, include an explicit long-press stop and accept that some gestures still work. Chrome on Android. A long-press on an image triggers a similar native menu. The handler reduces the frequency but doesn't eliminate every case; adding draggable="false" and -webkit-touch-callout: none helps but remains non-binding. Iframes. A script inside one origin cannot reach cross-origin iframe content; the parent page's handler applies only to the iframe element itself, not its inner document. You have no way to suppress the menu inside a third-party embed. Keyboard and assistive technology. Screen readers expose their own navigation menus, and Windows users have Shift+F10 and the menu key. The Level 2 handler covers the common keyboard cases; VoiceOver or NVDA gestures that open browser menus remain reachable. Extensions. A browser extension can synthesize a context menu or access the DOM directly. No page JavaScript can prevent that.
The practical consequence: treat right-click suppression as UI sugar, not security. It deters the accidental click and signals intent; it does nothing against anyone who wants the content.
What to use when you actually need to protect content
If the goal is protecting quality images, a high-res photograph stored at its full resolution in a tag is already available to anyone with a network tab. Three steps beat a contextmenu handler for photos:
1: Downscale. Serve a 1200-pixel-wide web derivative and keep the 6000-pixel original off the public server. Pixel resolution on screen is the fastest thing to lose and the hardest to restore. 2: Tile or overlay. Split the image into CSS-positioned tiles, or overlay a faint watermark with a transparent . Screenshots still capture it; casual right-clicking doesn't produce a usable clean file. 3: Serve protected originals only after authentication. Put full-resolution files behind a session check on the server. The viewer's request is the permission, not the browser menu.
For quizzes, exam answers, or anything a competitor could copy, the DOM is a public file. Any content rendered to the page can be extracted, and the right-click handler merely raises the smallest possible bar. If the answer must be secret, don't send it to the browser until the answer is revealed.
For a kiosk or full-screen presentation, a handler has real value: it prevents accidental menu openings during a sales demo. Pair it with browser kiosk mode and a locked presentation shell, and the block is a genuine convenience rather than a false promise.
Testing checklist and common failures
Run this sequence after any change; it catches most silent breakage in a few minutes.
1: Reload with cache bypass (Ctrl+Shift+R). Stale scripts are the most common cause of "it still shows the menu." 2: Right-click a paragraph, a link, and an image. All three should suppress the menu if your handler is document-wide. 3: Try the keyboard menu key and Shift+F10. The Level 2 block catches these; the inline-only version doesn't. 4: Use the device toolbar to check a touch emulation. If the menu still appears on long-press, that's the mobile gap, not a regression. 5: Confirm the rest of the page still works. Text selection, clicks, scrolling, and form fields (especially textareas where users might expect spellcheck on right-click) should behave normally.
When it fails, check the fizz rather than the code. If the event listener error is in the console, the handler was attached before the element existed. If it works on desktop and not mobile, you've hit the platform gap and should not expect otherwise. If it works on one page and not another, the script include is missing from the problematic template.
A quick health check you can run in DevTools:
Right-click the page. If the log fires without a menu appearing, your blocking handler is active. If the log doesn't fire at all, the event never reached the document—likely because the element has its own handler stopping propagation, or because you're in an iframe.
Accessibility, legal, and browser-assurance considerations
Blanking the context menu removes a familiar tool from real users. Keyboard-only visitors can still open menus via the menu key or screen-reader shortcuts, so they aren't fully blocked, but mouse-only users with motor disabilities who rely on right-click for "Open link in new tab" or spellcheck lose a path. If the page hosts tools where right-click is expected—spreadsheet editors, code playgrounds, map canvases—a document-wide block will generate support tickets.
There's also a legal dimension in some jurisdictions: blocking copy protections on publicly funded websites can conflict with accessibility requirements. For an informational site aimed at the public, consider whether the right-click suppression serves users or merely looks protective. A policy page explaining reuse terms often does more to set expectations than a technical clamp.
On the assurance side, no browser vendor guarantees this behavior permanently. Chrome, Firefox, and Safari have all adjusted how they route contextmenu on touch, and the handler is only as good as each release allows. If a hard guarantee matters, say for a paid content viewer, you need server-side controls and a license rather than an event listener.
The bottom line is that right-click suppression is a three-line, low-risk enhancement for a specific moment—kiosk demo, accidental-click prevention, a light community standard. It is not a security mechanism, and treating it as one is where most implementations go wrong. Add the handler for the friction it creates honestly, and invest in image downscaling, server controls, or access gating wherever the actual stakes are real.