Reputation: 2402
Most modern browsers support the commands ctrl+click
or command+click
or similar to open links in a new tab or a new window.
In an application, I want the link to get disabled on clicking. But only, if the target is the same window (if it, for instance, opens in a new tab, I don't want the link to become disabled since it is reasonable to click it again).
I did some tries and debugged the event object that gets created on click - but I cannot find any information about whether the target is a new tab or a new window.
Known workaround: Certainly it is possible to check whether a certain key had been pressed while clicking a specific link, an easy thing - but since these commands vary from browser to browser and from OS to OS, one would have to define a complicated mapping and who nows exactly what the user has configured and so on.
Is there any reliable way of determining, whether the location should be opened in a new tab or window?
Upvotes: 9
Views: 4817
Reputation: 664434
You can't prevent a browser to open a link in a new tab or window. There might be (undetecteable) configuration settings which force that, or think about mouse gestures: Right klick and moving down will open a new tab in Opera, and I guess this is undetectable.
Also, forcing a browser to open a tab in a new window (target="_blank"
or even window.open
) is considered bad style. You should let the user choose what he wants to do. If you really want to do, you also might consider window.onunload
which should be working most times.
Upvotes: 0
Reputation: 3421
Some times it is set by the browser settings to open links in new tab or in the same window.
You can check the target attribute of the link object.
<a href="http://your_link_to_page" target="_blank" >This open in new window</a>
Use javascript to manipulate the anchor tags at the page load and check for the target attributes. If the target value is set to _blank then enable them or you can perform this check when user clicks the link.
You can use a mechanism to find the CTRL is pressed when the link is clicked. There are some resources like this. You can build a system.
NOte: if you are using JQuery use different classes to categorize links that have to be checked. And disable them on ready state or at time the links are clicked.
Upvotes: 0
Reputation: 2618
Here you have a working example: http://jsfiddle.net/pioul/eCuNU/4/
This script will allow the click on the link only if the link is going to open in a new tab/window, based on the target="_blank"
attribute.
HTML:
<a href="http://google.fr" target="_blank">new window</a>
<a href="http://google.fr" target="_self">same window 1</a>
<a href="http://google.fr" target="">same window 2</a>
jQuery:
$("a").bind("click", function(e){
if($(this).attr("target") != "_blank"){
e.preventDefault();
}
});
EDIT:
You can take into account ctrl+clicks like this:
$("a").bind("click", function(e){
if($(this).attr("target") != "_blank"){
if(!e.ctrlKey){
e.preventDefault();
}
}
});
Upvotes: 5