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.
source share