dpd7195
dpd7195
2012-04-10 14:34
浏览 149
已采纳

如何拒绝/阻止用户在浏览器的新标签页中打开我网站中的链接?

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douzhaiya3968
    douzhaiya3968 2012-04-10 14:42
    已采纳

    This can't be done. Simple. Not possible

    点赞 评论
  • doudiaozhi6658
    doudiaozhi6658 2012-04-10 14:39

    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.

    点赞 评论
  • dongmeng9048
    dongmeng9048 2012-04-10 14:42

    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>
    
    点赞 评论
  • doucheng7534
    doucheng7534 2012-04-10 15:05

    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.

    点赞 评论

相关推荐