How to force the opening of a link in another tab and not window?

10

I have a button (anchor) that needs to be 'hidden', or no text at all. His structure is this:

<a class="testeimprimir" href="javascript:teste1();" target="_blank">TESTE</a>

The function is this:

function teste1() {
    window.open('www.google.com.br', '_blank');
}

If I put $('.testeimprimir').click() , the href event does not fire and I can not put it as onclick because by doing the same command $('.testeimprimir').click() it will open a new window and I need it open in a new ABA. Is there any command to run href with JavaScript or jQuery?

    
asked by anonymous 07.11.2014 / 12:47

4 answers

12

As mentioned in this answer , it is the user who decides if the new windows go to a flap or even a window.

The concept currently used by browsers is that new window requests are opened as a new tab unless you set otherwise in your browser options (note that this is not a standard).

It seems to me that you are trying to resolve something that can not be solved programmatically since the decision is by the user and is present in the browser settings to which we do not have access programmatically.


Force open in new tab

So far there is no official support yet, but in CSS there is a proposal to deal with this issue:

.novaAba {
    target-new: tab ! important
}

Some tests show that recent browsers support this property.

Learn more at CSS3 Hyperlink Presentation Module .


Force to open in new window

Unless you have set your browser to always open in a new tab, the following code opens in a new window because we are actually indicating details such as height, details that tell the browser that we want a new window and not a tab:

window.open("http://www.google.com/", "minhaJanela", "height=200,width=200");

These and other options in the new window can be found here .

    
07.11.2014 / 17:56
12

Your HTML and JavaScript are redundant. target="_blank" in HTML already forces the link to open in a new window, so JS, which does the same thing, is unnecessary. You could simply use HTML, like this:

<a class="testeimprimir" href="http://google.com" target="_blank">TESTE</a>

Now, if the page will open in another window or another tab, it is a browser decision. Chrome, for example, often forces a new tab. This is configurable in every browser, and there is no way to control, in JS or HTML, whether the opening will be in another flap or another window.

    
07.11.2014 / 13:00
4

Your code is almost right, just missing an http: // in front of the google link, see how I put it here and it worked:

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script>
      function teste1() {
        window.open('http://www.google.com.br', '_blank');
      }
    </script>
  </head>

  <body>
    <a class="testeimprimir" href="javascript:teste1();" 
    target="_blank">TESTE</a>
  </body>

</html>

If you run it here it will not work because Stack Overflow blocks page redirection so I put it in Plunker at a glance Plunker

    
07.11.2014 / 13:03
1

You can also make good use of this, just as I did:

window.open("url", "_blank");

Source: link

    
15.11.2016 / 17:34