How to Disable Text Selection and Right-click on a Web Page in JavaScript

In today’s digital world, the website is a crucial aspect of a business’s online presence.

As a website owner, it’s necessary to protect your content from being copied or stolen.

In this Javascript tutorial, we will look at how to disable text selection and right-click on a web page using JavaScript.


Disabling Text Selection

Text selection is an essential aspect of a website’s user experience, but at the same time, it’s necessary to protect the content from being copied.

To disable text selection, we can use the following code:

<style>
body {
  -webkit-user-select: none; /* Safari */
  -moz-user-select: none; /* Firefox */
  -ms-user-select: none; /* Internet Explorer/Edge */
  user-select: none; /* Standard */
}
</style>

Disabling Right-Click

Right-click is another way for a user to access a website’s content.

To disable right-click on a web page, we can use the following code:

<script>
document.addEventListener("contextmenu", function(e){
  e.preventDefault();
}, false);
</script>

Conclusion

In conclusion, protecting your website’s content is crucial to your online presence.

Disabling text selection and right-click can help you protect your content from being copied or stolen.

The above code examples can be easily implemented in your website to ensure the security of your content.

Disabling text selection and right-click may affect your website’s user experience.

It’s important to weigh the benefits and drawbacks before implementing these features on your website.