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.