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

Asked

Viewed 80,887 times

12

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

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

The Function is this:

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

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

  • 1

    Command to open a new tab? I don’t know this feature in any programming language.

  • Only the <a href="www.google.com" target="_Blank">open</a> opens in a new TAB if you click, same thing the window.open('www.google.com', '_Blank'), but the last one opens in a new WINDOW if the method is calling from an onClick no <a onclick='method()'></a>

4 answers

14


As stated in this answer, the user decides whether the new windows go to a tab or even a window.

The concept currently employed by browsers is that requests for new windows are opened as a new tab unless you set the opposite in your browser options (note that this is not a standard).

It seems to me that you are trying to solve something not programmatically solvable since it is the decision of the user and is present in the browser settings to which we do not have programmatically access.


Force open in new tab

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

.novaAba {
    target-new: tab ! important
}

Some tests reveal that recent browsers support this property.

Learn more in CSS3 Hyperlink Presentation Module.


Force open in new window

Unless you have set your browser to always open in a new tab, the following code allows you to open in a new window because we are effectively indicating details such as height, details that indicate to 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 consulted here.

13

Your HTML and Javascript are redundant. The target="_blank" in HTML already forces the opening of the link 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, whether the page will open in another window or another tab is a browser decision. Chrome, for example, usually forces a new tab. This is configurable in each browser, and there is no way to control, in JS or HTML, whether the opening will be in another tab or another window.

  • That’s true! But my case is this: I want the <a> click event to be called by Jquery or Javascript like this: $('.testeprint'). click(), as it will only happen in the Success of an AJAX. If I put the test method 1() in Success it will work, but it will open in a NEW BROWSER WINDOW and I need it to open in a new TAB and window.open line('www.google.com', '_Blank'); _Blank does not work, at least for me.

  • Almost that, I need in href come a javascript:Call(), or put the whole method in this line, whatever.

  • I would avoid javascript on href. It’s bad practice... Try to define what happens when you click elsewhere (with jQuery, using a $('#id-do-link').click(function(){ /* AQUI */ }))

5

Guy your code ta practically right, just missed an http:// in front of the google link, see how I put here and it worked:

<!DOCTYPE html>
<html>

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

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

</html>

If having run here won’t work because the Stack Overflow blocks page redirection then I put it in Plunker for a look Plunker

  • It works for me too, but only if I go and click with the mouse, what I need is to call the click with Javascript or Jquery, but it’s not working.

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.