window.open打开弹出而不是窗口 - 行为不一致

时间:2022-10-22 15:30:19

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:

在我的javascript代码中,我使用window.open方法并且行为完全不一致:根据我编写代码的方式,它将打开一个新选项卡或打开一个弹出窗口(被弹出窗口阻止程序阻止) 。由于你的帮助,我不知道如何解决它。 HTML代码:

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

The javascript code:

javascript代码:

function quoteVisu(){
  quoteCreate(1);
} 

quoteCreate is a method with an AJAX call

quoteCreate是一个带有AJAX调用的方法

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:

和goShow方法:

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.

上面的代码给出了一个弹出窗口,它不是预期的行为。如果我把window.open放在quoteVisu方法中,例如我将打开一个标签而不是弹出这是我想要的。但是,如果我把它放在那里,我没有新窗口网址所需的JSON的答案。

Thanks for your help.

谢谢你的帮助。

1 个解决方案

#1


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:

根据W3 ​​HTML5建议书,window.open应该在调用时打开一个窗口。[1]但是,它只表示应该打开辅助“浏览上下文”:

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

Window对象上的open()方法提供了一种机制,用于导航现有的浏览上下文或打开和导航辅助浏览上下文。

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

.open方法确实提供了一个目标字段,您可以使用此字段中的关键字来设置目标浏览内容:

             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

_blank new _self current _parent如果没有父级当前_parent如果父级也是*父级/*_parent如果有一个而且它不是*父级_top如果top是当前当前_top如果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/:

总之,无法控制是否显示选项卡或窗口。这不是你能控制的东西。这是一个例子: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.)

没有计时器的链接似乎是一致的:每次在Chrome上打开一个标签。另一方面,具有计时器(异步)的那个永远不会一致(在Chrome上)。第一次打开窗口时,下次单击“运行”后再次尝试时,会打开一个选项卡。没有办法确保它每次都打开一个标签。 (通常常见的浏览器会打开一个窗口,如果它是由异步函数调用的。)

#1


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:

根据W3 ​​HTML5建议书,window.open应该在调用时打开一个窗口。[1]但是,它只表示应该打开辅助“浏览上下文”:

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

Window对象上的open()方法提供了一种机制,用于导航现有的浏览上下文或打开和导航辅助浏览上下文。

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

.open方法确实提供了一个目标字段,您可以使用此字段中的关键字来设置目标浏览内容:

             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

_blank new _self current _parent如果没有父级当前_parent如果父级也是*父级/*_parent如果有一个而且它不是*父级_top如果top是当前当前_top如果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/:

总之,无法控制是否显示选项卡或窗口。这不是你能控制的东西。这是一个例子: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.)

没有计时器的链接似乎是一致的:每次在Chrome上打开一个标签。另一方面,具有计时器(异步)的那个永远不会一致(在Chrome上)。第一次打开窗口时,下次单击“运行”后再次尝试时,会打开一个选项卡。没有办法确保它每次都打开一个标签。 (通常常见的浏览器会打开一个窗口,如果它是由异步函数调用的。)