0

I am trying to open a link in new tab after the success method has been called.

Since, I am working for the project, I couldn't share the code. But I am sharing my scenario.

HTML Part:

<button id="news" onclick="getNews()">Get News</button>
<a href="#" id="newTabLink" target="_blank" ></a>

Javascript Part

function getNews(){
    $.ajax({
         url: "controllerName/actionMethod", 
         success: function(result){

                    //Assigning url to href
                    $('#newTabLink').href(result.url);

                    // Clicking the hyperlink
                    $('#newTabLink').click();
                   }
  });

}

My Scenario of the page:

On clicking the Get News button, it will fetch news from the database if there is any news present. Otherwise, it will show a pop-up on the same screen like "News not found".

Now, if there is any news present then, it will return the URL in ajax success method and I want to open it in new tab and not in new window.

NOTE: I don't want to use async:false in ajax setup. Since it stops the page until the response came from the server.

What I tried:

I tried to create an hyperlink on page loading(statically) and I assigned the href in that link on ajax success . After href is assigned, I am clicking that hyperlink.

My problem:

It is opening in new window not in new tab.

I want to open it in new tab without async:false and also note that, I don't want to open the new tab at the first of the button click and setting the timeout. That will become awkward for my project.

Vikash
  • 857
  • 1
  • 13
  • 32
  • 2
    Possible duplicate of [Open a URL in a new tab (and not a new window) using JavaScript](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-and-not-a-new-window-using-javascript) – Sojtin Feb 26 '16 at 07:53
  • In that, which one do you prefer for my scenario?. I found nothing matches my scenario. Could you please tell me that? – Vikash Feb 26 '16 at 07:55

2 Answers2

0

Source target="_blank" opens a new window in IE9, instead of a new tab May be check your browser settings , target_blank usually opens a new tab these days. Explanation for IE You need to use the target="_blank" attribute to make links open in a new window or tab. Where the link actually opens is up to the browser settings. So if you have Tools > Internet Options > Tabbed Browsing Settings > "Always open pop-ups in new tabs" selected, a target="_blank" link will open in a new tab. Note that this type of link will open in a new window by default on most browsers.

Community
  • 1
  • 1
REDEVI_
  • 684
  • 8
  • 18
0

You can look at link http://getbootstrap.com/javascript/#tabs.

Bootstrap docs.

$('#myTabs a[href="#profile"]').tab('show') // Select tab by name
regex
  • 101
  • 1
  • 9