Redirect in new tab when href is not known prior
<p>I have a link and a function which listens and gets triggered when that link is clicked.</p>

<p>The problem is the link is unknown in advance. Therefore, in this function an ajax request is made to the server to retrieve it. There is a strict requirement that the link must only be generated if requested by the user, and cannot be generated before hand.</p>

<p>How do you then redirect the user in a new tab to that link?</p>

<p><strong>Rewording</strong></p>

<p>So assuming I have </p>

<pre><code>&lt;a href="unknown" target="_blank"&gt;My Link&lt;/a&gt;
</code></pre>

<p>And there is an event handler that gets triggered on click.</p>

<p>I've tried several different approaches. The first time</p>

<pre><code>$('a[href="unknown"]').click(function(ev) {
 $.ajax({
      type: "GET",
      url: "/get_link", 
      context: document.body
    }).done(function(response) { 
      link = response.link

      //substitute href of a to the new link

  });
});
</code></pre>

<p>Realized that this didn't work because ajax is asynchronous so the user will get redirected to unknown first. Then when the response is received the href will be substituted with the correct link.</p>

<p>So then I tried making ajax async:false, but still that didn't work and I wasn't comfortable locking the browser.</p>

<p>Lastly, I tried suppressing the default behaviour, and then opened a new window (but I was unable to open a new tab using jquery).</p>

<pre><code>$('a[href="unknown"]').click(function(ev) {
 ev.preventDefault();
 $.ajax({
      type: "GET",
      url: "/get_link", 
      context: document.body
    }).done(function(response) { 
      link = response.link

      //substitute href of a to the new link
      window.open(link)


  });
});
</code></pre>

<p>Apparently, after googling around you can't open a new tab in jquery because it is based on the user's browser settings. </p>

<p>How do you then redirect the user in a new tab when they click a link (unknown in advance) which triggers a javascript function that retrieves the link using an ajax request?</p>
