Andrei Cristian Prodan
Andrei Cristian Prodan

Reputation: 1122

How do I deny/prevent users opening links in my site in new tabs on their browser?

There's a javascript generated link and I need to make sure the users can't open it either by not being able to right click and choose open in new tab, or by making that action do nothing at all.

Any ideas?

Edit: I didn't raise up the question to ask whether or not it's right to do it, but to know if it's possible. If you can help me with an appropriate answer please do, otherwise don't give me suggestions to problems I don't need solved. Sorry for the attitude.

Upvotes: 0

Views: 1485

Answers (4)

apsillers
apsillers

Reputation: 115950

Your best solution might be to avoid using a link at all. Instead, use a <span> or <div> (or similar) and bind a click event handler that redirects the page.

document.getElementById("mylinkdiv").addEventListener("click", function() {
    window.location.href = "foo.html"
}

In general this is bad, because you don't want you site functionality to depend on JavaScript being enabled, but you've already said that the user needs JavaScript to see the link in the first place.

Upvotes: 2

Manish Kumar
Manish Kumar

Reputation: 21

You can do this by disabling right click on your page. Since "open link in new tab" is a browser specific feature you can't selectively disable that feature in right click menu. Put this script in your web page to disable right click.

<script language=JavaScript>
     var message = "You cannot right Click on this page"; 
     function rtclickcheck(keyp){ if (navigator.appName == "Netscape" && keyp.which == 3)
     {
       alert(message); 
       return false;
     } 
     if (navigator.appVersion.indexOf("MSIE") != -1 && event.button == 2) 
     {
       alert(message);
       return false;
     }
 } 
   document.onmousedown = rtclickcheck;
</script>

Upvotes: -2

Habib
Habib

Reputation: 223277

This can't be done. Simple. Not possible

Upvotes: 1

Berry Langerak
Berry Langerak

Reputation: 18859

There's a javascript generated link and I need to make sure the users can't open it either by not being able to right click and choose open in new tab, or by making that action do nothing at all.

Can't be done. To be honest, I can't see a legitimate reason for anyone to want this either. The result of opening a page in a tab is no different from just opening the page. Anyway, there's no way to tell whether your site has been opened in one tab, or in different tabs.

Upvotes: 3

Related Questions