Disable Opening New Window or New Tab by holding down on a link

前端 未结 4 710

I want to thanks those who are currently reading this and for your time. Let\'s go straight to the topic!

I am currently trying to disable a feature where a user sho

相关标签:
4条回答
  • 2021-01-19 07:20

    You could bind an event handler to the desired link element and use jQuery's .preventDefault() method to cancel the link going anywhere. Your code would look something like this:

    $('a').click(function(event){
      event.preventDefault();
    });
    
    0 讨论(0)
  • 2021-01-19 07:25

    CSS:

    a{
       color: blue;
       cursor: pointer;
       text-decoration: underline;
     }
    

    Script:

    var links = document.getElementsByTagName("a");
    for(var i=0; i < links.length; i++){
        links[i].setAttribute("data-href", links[i].getAttribute("href"));
        links[i].removeAttribute("href");
        links[i].onclick = function(){
            window.location = this.getAttribute("data-href");
        };
    }
    
    0 讨论(0)
  • 2021-01-19 07:28

    You'd probably want to use some JavaScript to load the new contents into the existing page. But that will mean that your site will not have any history (unless you add it in manually), so it will break navigation, and then there's no guarantee that the user will just copy and paste the URL into a new window or tab anyway.

    In essence if you need to disable a fundamental Operating System (yes, for websites the Browser is the Operating System) action then you're doing something wrong. (In your design or intended behaviour.)

    0 讨论(0)
  • 2021-01-19 07:31

    If you need to prevent the user from opening links in a separate tab or window, then there's probably something wrong with your site design. Why do you feel that this restriction is necessary?

    But to answer your question -- one way to more or less guarantee this, without depending on JavaScript and/or browser quirks, and without breaking too many any other important browser features, is to have each "link" actually be a form-button: <form method="GET" action="http://example.com/..."><input type="submit" value="click here for more information" class="pseudo-link-input" /></form>, with lots of CSS styling to make the button look like a link.

    Caveats:

    • This is a really mean thing to do to your users!
    • If any of your links are to pages with query strings, then you'll have to translate those query strings into <input type="hidden" .../> elements. (And this requires that the query strings be of the type that can be constructed by an HTML form.)
    • Technically a browser could give users to the option to open a given form result in a new window, but in practice I don't know of any that do.
      • Of course, once they've clicked the link, and the pages are in their history, they can visit the URL in whatever tab/window they want. Some browsers, such as Firefox, even let users open "Back" or "Forward" in a new tab, so this wouldn't be a lot of effort.
    • At least in some browsers, this will also prevent users from selecting and copying the text of a link. If they copy the text of the page, all the link text is liable to silently vanish.
    • And it will probably cause various other problems as well. (Hacking around basic browser features is never a good idea.) The above caveats are just the first ones that came to mind.
    0 讨论(0)
提交回复
热议问题