Disable opening a new window or a new tab while holding the link

I want to thank those who are reading this now and for your time. Go straight to the topic!

I am currently trying to disable a feature in which the user cannot open the link to a new window or tab while holding the link.

Any suggestions or answers to resolve this issue are welcome. Thanks again!

+4
source share
4 answers

You can associate an event handler with the desired link element and use the jQuery .preventDefault() method to cancel the link anywhere. Your code will look something like this:

 $('a').click(function(event){ event.preventDefault(); }); 
+6
source

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

But to answer your question - one way to more or less guarantee this, regardless of the features of JavaScript and / or the browser, and without violating too many other important functions of the browser, is that each "link" is actually 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 more CSS styles to make the button look like a link.

Cautions:

  • This is really the most interesting for your users!
  • If any of your links refers to pages with query strings, you will have to translate these query strings into <input type="hidden" .../> elements. (And this requires the query strings to be of a type that can be created using an HTML form.)
  • Technically, the browser can provide users with the opportunity to open the result of this form in a new window, but in practice I do not know anything like this.
    • Of course, once they click on the link and the pages are in their history, they can visit the URL in any tab / window that they want. Some browsers, such as Firefox, even allow users to open Back or Forward in a new tab, so this will not be a lot of effort.
  • At least in some browsers, this will also prevent users from selecting and copying link text. If they copy the text of the page, all link texts may disappear unchanged.
  • And this is likely to cause other other problems. (Hacking around basic browser features is never a good idea.) The above disclaimers are just the first ones that come to mind.
+3
source

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"); }; } 
+1
source

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

In essence, if you need to disable the underlying operating system (yes, for websites, the browser is the operating system), then you are doing something wrong. (In your design or intended behavior.)

0
source

All Articles