Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How do I disable right click with a tooltip appearing when clicked?

Edrol97

Bronze Coder
Hi all, I've seen some posts recommending this shouldn't be done on Google, but want it for my website as I think it's a cool feature to put a copyright into but also maybe a quirky little message. Does anyone have ideas of how to do this? I know I will add an eventlistener of sorts and an onclick event but I'm not really sure further from this.
 
Hi all, I've seen some posts recommending this shouldn't be done on Google, but want it for my website as I think it's a cool feature to put a copyright into but also maybe a quirky little message. Does anyone have ideas of how to do this? I know I will add an eventlistener of sorts and an onclick event but I'm not really sure further from this.
Hello,

So you can do this a couple different ways. One way is an example below. This technique does not prevent users from accessing the page's source code or elements through developer tools. For sensitive content, consider server-side protections or DRM solutions.

HTML:
<div id="tooltip" style="display: none;">Right-click is disabled! Your copyright info here</div>

CSS:
#tooltip {
  position: fixed;
  background-color: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 5px 10px;
  border-radius: 5px;
  font-size: 12px;
  z-index: 1000;
}

JavaScript:
document.addEventListener("contextmenu", function (event) {
  // Prevent the default context menu
  event.preventDefault();
 
  // Get the tooltip element
  const tooltip = document.getElementById("tooltip");
 
  // Set the tooltip position near the cursor
  tooltip.style.left = `${event.pageX}px`;
  tooltip.style.top = `${event.pageY}px`;
 
  // Display the tooltip
  tooltip.style.display = "block";

  // Hide the tooltip after 2 seconds or set your own time you would like it to show for here.
  setTimeout(() => {
    tooltip.style.display = "none";
  }, 2000);
});


Hope this solution helps! Let us know if you need anything else!
 
Hey there!
If you want to add a fun message when users right-click on your site here is one-way to do it:

1. HTML:
Add a hidden tooltip:
HTML:
<div id="tooltip" style="display: none;">Right-click is disabled! Your copyright info here.</div>

2. CSS:
Here is how to style the tooltip:
Code:
#tooltip { 
  position: fixed; 
  background: rgba(0, 0, 0, 0.8); 
  color: #fff; 
  padding: 5px 10px; 
  border-radius: 5px; 
  font-size: 12px; 
  z-index: 1000; 
}

3. JavaScript:
Here is the script to display the tooltip on right-click:
JavaScript:
document.addEventListener("contextmenu", (event) => { 
  event.preventDefault(); 
  const tooltip = document.getElementById("tooltip"); 
  tooltip.style.left = `${event.pageX}px`; 
  tooltip.style.top = `${event.pageY}px`; 
  tooltip.style.display = "block"; 
  setTimeout(() => tooltip.style.display = "none", 2000); 
});

Do tell if you require more of my help 🙂
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom