Determine onclick whether the clicked link will open a new window or tab

Most modern browsers support the ctrl+click or command+click commands or look like open links in a new tab or in a new window.

In the application, I want the link to be disabled when clicked. But only if the target is the same window (if it opens, for example, in a new tab, I do not want the link to be disabled, since it is reasonable to click it again).

I made several attempts and debugged the event object, which is created by clicking, but I can not find any information about whether the target is a new tab or a new window.

Known workaround. Of course, you can check whether a certain key was pressed when clicking on a specific link, but this is not so, but since these commands vary from browser to browser and from OS to OS, it would be necessary to define a complex display and which accurately determines what the user has configured etc.

Is there any reliable way to determine if a location should be opened in a new tab or window?

+6
source share
3 answers

Here you have a working example: http://jsfiddle.net/pioul/eCuNU/4/

This script will allow you to click the link only if the link opens 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 consider ctrl + clicks like this:

 $("a").bind("click", function(e){ if($(this).attr("target") != "_blank"){ if(!e.ctrlKey){ e.preventDefault(); } } }); 
+3
source

Several times it is set by the browser settings to open links in a 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 manage anchored tags when loading the page and check the target attributes. If the target value is set to _blank, enable them or you can perform this check when the user clicks on the link.

You can use the mechanism to find the CTRL hit when the link is clicked. There are resources like this . You can create a system.

NOte: if you use jQuery, use different classes to classify the links you need to check. And turn them off in a ready state or when you click on the links.

0
source

You cannot prevent the browser from opening a link in a new tab or window. There may be (undetectable) configuration options that make it or think of mouse gestures: Right clicking and moving down will open a new tab in Opera, and I think it's impossible to detect.

In addition, forcing the browser to open a tab in a new window ( target="_blank" or even window.open ) is considered bad. You must let the user choose what he wants to do. If you really want to do this, you can also consider window.onunload , which should work in most cases.

0
source

Source: https://habr.com/ru/post/902850/


All Articles