-1

I am working on a spring mvc project where there is one scenario that on click of a button view is displayed in the same tab. but in certain scenario it is redirected to another url. so at this time when there is redirect i want to open in new tab.

conclusion : I want to know whether there is any way to open in new tab if there is url redirection otherwise open in the same tab.

Avyaan
  • 1,285
  • 6
  • 20
  • 47
  • 1
    What do you mean by "URL redirection" ? Also, as a side remark, you shouldn't choose for your users and let them decide if they want the link to open in a new tab/window or not. – Laurent S. Jun 17 '15 at 09:11
  • You could do something like [this](http://stackoverflow.com/questions/8571227/use-javascript-to-check-http-status-codes) to check the response code. After that decide whether to open a new window or not. – LSA Jun 17 '15 at 09:13
  • @Bartdude my question is simple, i just want to know if there is redirect in code, it should be displayed in new tab. – Avyaan Jun 17 '15 at 09:13
  • Yeah your question is simple when you don't know much but I can probably think about 5 or 6 ways to do a redirection, was it from server or client-side. So that's not THAT simple. Which kind of redirection are we talking about here ? – Laurent S. Jun 17 '15 at 09:16
  • @Bartdude server side. – Avyaan Jun 17 '15 at 09:18
  • Then remark from LSA points you to the correct direction : catch the click event on anchor, perform a request to the URL, get status code and act accordingly whther this is a redirection or not... – Laurent S. Jun 17 '15 at 09:22

1 Answers1

0

This will alter all your links, adding target="_blank" to them if they do not share your domain, if that is what you mean:

var slice = location.href.split('/');
var domain = slice[1] ? slice[0] : slice[2];
$('a').each(function()
{
    if (this.href.indexOf(domain) < 0)
    {
        $(this).attr('target','_blank');
    }
});
omikes
  • 8,064
  • 8
  • 37
  • 50