How u can stop somebody from viewing the source code of the webpage in browser

Ali Hasan
2 min readMar 31, 2023

--

Protect Your Webpage by Disabling the View Source Code Option

As a developer, you might want to prevent others from viewing the source code of your webpage for security reasons or to protect your intellectual property. However, it is not possible to completely prevent someone from viewing the source code of a webpage, as it is downloaded to the user’s browser and therefore accessible.

i’ll discuss how you can prevent viewers from accessing your webpage source code in browsers.

Disable the View Source Code Option

One of the easiest ways to prevent viewers from accessing your webpage source code is to disable the View Source Code option in browsers. This can be done by adding a simple script to your webpage that disables the right-click context menu, which is the primary way viewers can access the View Source Code option.

// Disable right-click context menu
document.addEventListener('contextmenu', event => event.preventDefault());

This script disables the right-click context menu, which contains the View Source Code option, and prevents it from being accessed.

Another way to disable

View Source Code option is to disable the F12 key and Ctrl+Shift+I combo, which opens the browser developer tools that include the View Source Code option. Here’s the script to disable them:

// Disable F12 key and Ctrl+Shift+I combo
document.addEventListener('keydown', event => {
if (event.keyCode === 123 || (event.ctrlKey && event.shiftKey && event.keyCode === 73)) {
event.preventDefault();
}
});

This script disables the F12 key and Ctrl+Shift+I combo and prevents viewers from accessing the browser developer tools, including the View Source Code option.

however, it’s worth noting that these methods do not completely prevent viewers from accessing your webpage source code. Advanced users can still find ways to access the code,

--

--

Ali Hasan
Ali Hasan

Responses (3)