0

In my javascript code I am using the window.open method and the behavior is totally inconsistent: depending on the way I write the code it will either open a new tab or open a pop up window (which is blocked by the pop up blocker). I don't know how to work around it thanks for your help. The html code:

<a class='btn btn-success' target="_blank" onclick="quoteVisu(); return false;">Visualiser</a>

The javascript code:

function quoteVisu(){
  quoteCreate(1);
} 

quoteCreate is a method with an AJAX call

function quoteCreate(num_function){
    var request=$.ajax({
    url: url_up,
    type: "POST",
    beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
    data: {q_param: {
             title: title, 
             total: total,
             list: list,
             client: idclient,
             tax_rate: tax_rate 
           }},
    success: function(data, textStatus, jqXHR) {
              if (num_function==1){request.done(goShow(data.quote_id))};
              if (num_function==0){request.done(goBack());}
            },
    dataType: "json"
    });
return true;
}

and the goShow method:

function goShow(quote_id) {
    var url_visu="/visu_pdf/quote_visu."+quote_id
    window.open(url_visu, '_blank');
    return true;
 }

The code above gives a pop up window which is not the behavior expected. If I put the window.open in the quoteVisu method for example I will have a tab open and not a pop up which is what I want. But if I put it there I don't have the answer from the JSON which is needed for the new window url.

Thanks for your help.

fro
  • 437
  • 4
  • 16
  • [You cannot control](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript) whether a tab or a window pops up. It's a native implementation. Behavior is expected to be inconsistency among browsers, and even among itself. – Derek 朕會功夫 Aug 23 '14 at 00:39
  • First of all, I’d remove `target="_blank"` from the link, it makes no sense here. And second of all, you might get different behavior simply based on the fact that due to the asynchronous AJAX call the opening of the window is “de-coupled” form the actual click event – that _might_ make a browser react differently. – CBroe Aug 23 '14 at 00:45
  • it seems like this is the issue but the fact there is not solution is pretty crazy, as I guess it is a common issue... – fro Aug 23 '14 at 01:27

1 Answers1

0

According to the W3 HTML5 Recommendation, window.open should open a window when called.[1] However, it only says a auxiliary "browsing context" should be opened:

The open() method on Window objects provides a mechanism for navigating an existing browsing context or opening and navigating an auxiliary browsing context.

The .open method does provide a target field which you can use keywords from this set to set the target browsing content:

             keyword                   ordinary effect

_blank new
_self current
_parent if there isn't a parent current
_parent if parent is also top parent/top
_parent if there is one and it's not top parent
_top if top is current current
_top if top is not current top

However this doesn't distinguish a tab or a window.

To conclude there is no way to control whether a tab or a window will appear. It's not something that's under your control. Here's an example: http://jsfiddle.net/DerekL/0amrodLx/:

function openTab(name){
    open("http://en.wikipedia.com", name);
}

function timer(n){
    setTimeout(function(){ openTab(n); }, 1);
}

<a href="#" onclick="openTab('Wikipedia')">Open Tab</a>
<a href="#" onclick="timer('Wikipedia')">Open Tab With Timer</a>

The link with no timer seem to be consistent: opens a tab every time on Chrome. On the other hand, the one with timer (async) is never consistent (on Chrome). The first time it opens a window, the next time I try after clicking Run, it opens a tab. There is no way to make sure it opens a tab every single time. (Generally common browsers open a window if it's called by an async function.)

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • Wow, great answer thanks. If I understand well, I cannot do anything on this issue because of the time of the ajax call answer, am I right? This is pretty disappointing but at least I don not have to spend time debugging anymore! – fro Aug 23 '14 at 01:26
  • @fro Actually there is no solution because not every browser has tabs and the general recommendation is trying to cover all cases. It's up to the browser itself to decide. – Derek 朕會功夫 Aug 23 '14 at 01:29