简体   繁体   English

jQuery-使用Window.Open()防止弹出窗口阻止程序

[英]jQuery - Prevent Pop Up Blocker Using Window.Open();

How can I avoid browser from Pop Up Blocker if I use jQuery to open in a new window. 如果我使用jQuery在新窗口中打开,如何避免使用弹出窗口阻止程序中的浏览器。 I been googling regarding this issue but still stuck with it. 我一直在搜索这个问题,但仍然坚持下去。 This is the codes that I made, 这是我编写的代码,

$(document).ready(function(){
  $('#newLink').click(function(event){
    event.preventDefault();
    var page = $(this).attr('href');
    $.post(page,
    {
        pr_no : pr_no
    })
    .done(function(data){
        window.open(page, "MsgWindow", "width=800,height=800");
    });
});

Popup blockers will typically only allow window.open if used during the processing of a user event (like a click). 弹出窗口阻止程序通常仅在处理用户事件(例如单击)时使用window.open。 In your case, you're calling window.open later, not during the event, because $.getJSON is asynchronous. 在您的情况下,您稍后要调用window.open,而不是在事件期间调用,因为$ .getJSON是异步的。

You have two options: 您有两种选择:

Do something else, rather than window.open. 做其他事情,而不是window.open。 Make the ajax call synchronous, which is something you should normally avoid like the plague as it locks up the UI of the browser. 使ajax调用同步化,这通常是您应该避免的事情,例如鼠疫,因为它会锁定浏览器的UI。 $.getJSON is equivalent to: $ .getJSON等效于:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

...and so you can make your $.getJSON call synchronous by mapping your params to the above and adding async: false: ...因此您可以通过将参数映射到上述参数并添加async:false来使$ .getJSON调用同步。

$.ajax({
    url:      "redirect/" + pageId,
    async:    false,
    dataType: "json",
    data:     {},
    success:  function(status) {
        if (status == null) {
            alert("Error in verifying the status.");
        } else if(!status) {
            $("#agreement").dialog("open");
        } else {
            window.open(redirectionURL);
        }
    }
});

Again, I don't advocate synchronous ajax calls if you can find any other way to achieve your goal. 再说一次,如果您能找到实现目标的其他方法,我不提倡同步ajax调用。 But if you can't, there you go. 但是,如果做不到,那就去吧。

Here's an example of code that fails the test because of the asynchronous call: 这是由于异步调用而导致测试失败的代码示例:

Live example | 现场示例| Live source (The live links no longer work because of changes to JSBin) 实时源(由于对JSBin的更改,实时链接不再起作用)

jQuery(function($) {
  // This version doesn't work, because the window.open is
  // not during the event processing
  $("#theButton").click(function(e) {
    e.preventDefault();
    $.getJSON("http://jsbin.com/uriyip", function() {
      window.open("http://jsbin.com/ubiqev");
    });
  });
});

And here's an example that does work, using a synchronous call: 这是一个使用同步调用起作用的示例:

Live example | 现场示例| Live source (The live links no longer work because of changes to JSBin) 实时源(由于对JSBin的更改,实时链接不再起作用)

jQuery(function($) {
  // This version does work, because the window.open is
  // during the event processing. But it uses a synchronous
  // ajax call, locking up the browser UI while the call is
  // in progress.
  $("#theButton").click(function(e) {
    e.preventDefault();
    $.ajax({
      url:      "http://jsbin.com/uriyip",
      async:    false,
      dataType: "json",
      success:  function() {
        window.open("http://jsbin.com/ubiqev");
      }
    });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM